Using escape and raw in twig? - symfony

I have an article that has smart quotes in it, so I need to escape the article content. However, if I use
article.body| e
to escape the article, the html does not show up as formatted. And if I use
article.body| raw
the article is displayed correctly as html but the smart quotes are not escaped in the content.
Does anyone know how to escape content while correctly displaying the html?

You can specify a strategy for the escape filter.
'html' won't work for you here because it internally uses PHP's htmlspecialchars
'html' is the default strategy when using {{ var|e }}
You will need to create a custom escaping strategy for your use case.
... but if it's only the quotes which need to be replaced you could do a simple preg_replace on PrePersist and PreUpdate using Doctrine's Lifecyle Events.

to show the special characters we can use like this {{ '<'|e }} it will print only angle angle bracket '<'

Related

Can pass HTML Special character as a og: description?

I giving a support for my friend website. In that site they use Regex pattern for filter tags and HTML special characters. Case of that quote and others symbols are missing when it comes as a special character (ex:- &rsquo &lsquo).
So do we need to remove HTML special character from og:description property field ?

New line inserted when passing a category link as a template parameter

I have a mediawiki template which contains the following code: (there is more, but this is the part relevant to the question)
[[{{#if:{{{page|}}}|{{{page|}}}|{{{ns|}}}{{{1}}}}}]]
I tried to use it where {{{ns}}} is :Category:, but the first colon on :Category: creates an automatic newline (see https://meta.wikimedia.org/wiki/Help:Newlines_and_spaces#Automatic_newline).
How can I fix this template so it correctly creates the link without the automatic newline?
I tried adding <nowiki/> before {{{ns|}}}, but then I got a plain text link ([[:Category:ParameterOne]]) instead of a linkified link
Fortunately I didn't need to add the initial colon in :Category:, so I was able to solve this by simply adding the colon at the beginning of the link inside the Template.
I.e. I passed Category: (without a colon as a prefix) as the parameter {{{ns}}}, and changed the code to:
[[{{:#if:{{{page|}}}|{{{page|}}}|{{{ns|}}}{{{1}}}}}]]
Simply adding a colon before the #if.
Fortunately all other namespaces worked the same as before.

Render double curly-brackets inside handlebars partial

How do you force handlebars.js to ignore (and not render) templates?
It's possible to escape the line with backslash
\{{>partialName}}
It's not mentioned in the documentation, but a look at the source-code documentation revealed this technique.
HTML entity might be a solution if you just wang to display your brackets. Try { and }.

Displaying code blocks in a WordPress tech blog

I am trying to display some HTML code in my blog post. Is there any way to show that HTML code as an element of that post?
You should go through the documentation about
Writing Code in Your Posts.
Whether you write plugins or hacks for WordPress, or you want to add bits and pieces of code about your own WordPress site or other programming code like HTML, CSS, PHP or JavaScript, putting code in your post that actually looks like code, but doesn't behave like code, is a frequent challenge for bloggers.
It's a nice article on how to display code in post/page of WordPress.
Super easy, and that WordPress Codex page Ram Sharma shared has great material to follow.
Put all your code within the <code></code> element and simply make sure to escape every opening less than symbol "<" and greater than symbol ">" though you might find yourself escaping other things:
Example:
<code><?php echo $var; ?></code>
But in place of the greater than & less than symbol I replaced them with the appropriate HTML character entities "& g t ;" & "& l t ;" no spaces.
Check out an example of writing code in your posts over at my Web Development & Design Blog, no plugin! Pure code!
Building off of Giancarlo Colfer's answer, you can use the <code></code> tags along with the <pre></pre> tags.
Example:
<code>
<pre>
<!-- code goes here -->
</pre>
</code>
Your code will be output exactly as entered.
Great answers so far, I just wanted to add a tip.
If you are blogging about code you have already written; use a text editor (I use Notepad++) with a built in find and replace (Ctrl+H) function.
Find: "<"
Replace: "& l t ;"
then
Find: ">"
Replace: "& g t ;"
As before, remove the spaces from the replace string.
Finally - remember to undo the find and replace and/or don't click save.

How to get a newline in JSF (plain text)?

I am using JSF to generate text and need newlines to make the text easier to read. I have an HTML version which works great, I hacked it together using <br/> (I'm not proud of that, but it works).
I would like to do the same for the plain text version such as inserting \n.
I am doing something like this:
<customTagLibrary:customTag>
<h:outputText value="Exception"/><br/><br/>
...
</customTagLibrary:customTag>
Instead of the <br/>, I want \n. What is the best way to do that?
Please keep in mind that I'm NOT using this to generate content that will be sent to the browser. This will be used to create email messages or (plain-text) attachments in emails.
Thanks,
Walter
If you use Facelets to render HTML, this did the trick for me:
<h:outputText value="
" />
Why not simply wrap it in a HTML <pre> tag?
The h: prefix means html. So if you don't want html, don't use h: tags. Create your own tags or at least renderers for h: tags and let them output \n.
But my personal opinion is that it's better to use another templating technology for emails.
I'm assuming that your template XML strips whitespace. Unfortunately, EL doesn't let you express newlines in string literals, but you could bind to a string that did (<h:outputText value="#{applicationScope.foo.newline}" />). However, since you want to serve multiple markups, this would be a less than ideal approach.
To share JSF templates between different content types, you could 1) remove all markup specific tags from the template and 2) provide RenderKits which would provide a Renderer appropriate for the current markup. This would be the way to serve content using JSF's model-view-presenter design.
You may have to make some decisions about how you handle markup-specific attributes. The default render kit is geared towards rendering the HTML concrete components. Exactly what you do depends on your goals.
I am going to simply write a newline tag. It will detect whether it should output a or a \n. In my tag library, it would look like this:
<content:newline/>
Walter

Resources