How to print formatted html using Twig? - html

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 }}

Related

How to render twig variable as HTML attribute name

I am trying to set a custom HTML attribute name using twig, trying to achieve this:
<div age="24"> Something </div>
This is my code:
{% set custom_age_attribute= "age" %}
<div {{custom_age_attribute}}="24"> Something </div>
But the twig code is not rendering and getting this:
<div {{custom_age_attribute}}="24"> Something </div>
Is there any way to make it work?

Jekyll Post Excerpt: How to remove the first paragraph

I know how to use to define the end of the excerpt, but I'd like to omit the first paragraph of the post as well.
How can this be done?
Thanks in advance all!
Just got into the same issue. I have a separate place for excerpt and below I want post without excerpt (because it was already displayed). What worked for me was:
<header>
{{ page.excerpt }}
</header>
<article>
{{ content | remove_first:page.excerpt }}
</article>
It has to be defined in the same layout you use for posts and posts only.
If I understand right you just want the first paragraph.
post.excerpt gets the first paragraph of your post. An alternative would be to create a variable in your markdown files and call it whenever you want.
e.g.
---
layout: post
title: Your title
post_description: A plugin to extend Swiper for easier JW Player integration.
---
and call {{ post.post_description }} in your lists.

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>

Escaping designated CSS class

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.