Escaping designated CSS class - jinja2

I'm trying to come up with a way to use jinja to escape a certain css class. Example:
<div class="code"> <center> ... more example html code ... </center> </div>
where the
<center>
tag is visibly written in the post. Again, I want ONLY the code class to escape. Any hints?

If you want ONLY your class code to be escaped, then you need to pass it in as a separate variable like so:
{{ classcode|e }}{{ Your other unescaped code here }}
I hope this helps you.

Related

How to print formatted html using Twig?

I pass into twig template variable that contains html code. When I print it I get something like that:
<div class="links"> <span class="logo"></span> homepage page 1 page 2 page 3 </div>
How do i make the code look more readable? Like this:
<div class="links">
<span class="logo"></span>
homepage
page 1
page 2
page 3
</div>
If you want to display the htmlas is for a code snippet I'd suggest you just wrap <pre> around it.
<pre>{{ html }}</pre>
demo
If you just want to preserve newlines and tabbing you could wrap the var with div and set the white-space to pre
<style>
div.pre {
white-space: pre;
}
</style>
<div class="pre">{{ html }}</div>
The source HTML is displayed exactly as you pass it in. One way I can think of to fix this specific issue is by using a solution like https://stackoverflow.com/a/34556900/4646470 to format the HTML and create a Twig filter/function 1 to handle it in Twig templates.
I am giving the sample example solution to your questions. I hope it helps you.
{{ title }} {{ entity_print_css }} {{ content }}
Sample node--article--pdf.html.twig
{{ content.field_name }} {{ content.body }} {{ label }} {{ content.field_name.0.value }}

Tumblr theme getting tags of a post in JavaScript

I want to use the tags of a post in JavaScript. To get the tags I was originally using something like the following:
<article class="post" data-tags="{TagsAsClasses}">
This worked fine up until I needed to use special characters like | which becomes a _ with this property and gives me the wrong tag.
So now I need to do something like (using the {block:Tags}):
<article class="post" data-tags="{block:HasTags}{block:Tags}{Tag}{/block:Tags}{/block:HasTags}">
Yet I foresee problems if {Tag} ever has a double quote in it.
Is there any way I can keep the data-tags attribute and still use {block:Tags} to get the real tag?
I have come up with a solution that involves adding and then reading a bunch of markup but I just don't like it as much.
<div class="postHiddenTags" style="display:none;">
{block:HasTags}
{block:Tags}<div data-tag={JSTag} data-tagURL={JSTagURL}></div>{/block:Tags}
{/block:HasTags}
</div>
If you need to get the tags in JavaScript, you can define them in a <script> tag from the start. In your theme HTML:
<script>
var tags = {};
{block:Posts}
{block:HasTags}
tags[{JSPostID}] = [
{block:Tags}
{
"tag": {JSTag},
"tagURL": {JSTagURL},
},
{/block:Tags}
];
{/block:HasTags}
{/block:Posts}
</script>
You have a global JavaScript dictionary, from a post's ID to its list of tags (and tag URLs). The "JS" prefix is to output the string wrapped in quotes.
In each of your post elements, add an attribute for the post ID:
{block:Posts}
<article data-id="{PostID}" class="post"></article>
{/block:Posts}
You can have {block:Posts} multiple times in your template, although the theme documentation does not mention this.
Whenever you need a post's tags, get the post ID from its DOM element, then look up the ID in the tag dictionary. This way you don't have to worry about escaping/un-escaping special characters.

Execute blade tags in textarea

I am setting up a simple CMS and I am using a Textarea (Froala to be exact) to render the HTML content of each page that's generated via database. The textarea can display the html with no problems. However, the blade template isn't executing. It's just in plain text. How can I make sure blade executes correctly? I need it mainly for links.
For an example. I have a form element written in blade:
{{ Form::text('name')}}
It should display like so on my page:
<input name="name" type="text">
However, it's just displaying like this:
{{ Form::text('name')}}
EDIT: here is my pages.blade.php file if they may help with figuring it out.
#extends('layouts.main')
#section('main_content')
<section class="mainContent">
<div class="row">
<h1 title="{{ $pages->title }}">{{ $pages->title }}</h1>
{{ $pages->body }}
</div>
</section>
#stop
There is no out-of-the-box way to do that. Blade templates are compiled only from files.
But you can take look at
Flynsarmy/laravel-db-blade-compiler if you want to compile Blade from Eloquent model
or
TerrePorter/StringBladeCompiler if you want to compile Blade from strings.

safe option in django templates

I am trying to print a html tag in django template using safe option in django templates.
I am using it like below.
{{ message|safe }}
It works fine as long as I have only one tag ,
for example If I have to print below, it works fine.
<div class="message">data</div>
But If I have another tag inside div , it is not generated as raw html, rather it is generated as below.
<div class="message"><img src="/media//uploads/Capture_16.PNG" /></div>
I would like it to be generated as
<div class="message"><img src="/media//uploads/Capture_16.PNG" /></div>
What options do I have to achieve this ?
EDIT
Below is the original message without using safeor any kind of escaping.
<div class="message">data</div>
<div class="message">&lt;img src=&quot;/media//uploads/Capture_16.PNG&quot; /&gt;</div>

Jade comments at the end of a tag

I want to know if it's possible to put comments in jade at the end of an html tag, in the output file.
example:
<div class="header">
....
</div> <!-- ./this comment -->
Probably not a good idea.
But if you really want it, just add a comment after the div.
It won't be on the same line as the </div>.
It sends more html down the pipe, so I wouldn't recommend it for production use.
.header
hello
//./header
If you use a //- comment, it will not make it into the browser's view source. Useful for private comments.