I'm using Grunt and Assemble to create precompiled templates on my site but I need to dynamically create some sections based on information stored in client-side storage. Is there a way to exempt sections of a template from precompilation?
We can't do custom delimiters (a shortcoming of Handlebars), but there are a couple of solutions that might work for you. Neither is necessarily idiomatic, so you be the judge of how suitable these are for the project you're working on.
string replacement
You could use some custom (temporary) delimiters for the templates in the content that shouldn't be compiled, then create a block helper that will convert those delimiters back to valid handlebars expressions during compile time (I tested this for this answer and it works):
For example:
Handlebars.registerHelper("raw", function(options) {
return options.fn(this).replace(/\[\[/g, '{{').replace(/\]\]/g, '}}');
});
Then use it like this:
{{#raw}}
{{> foo }}
{{/raw}}
Inside foo.hbs, you might have something like this:
<title>[[title]]</title>
and it will render to:
<title>{{title}}</title>
This solution is hacky for sure, and you might have problems if you happen have any code examples embedded in the included content and one of the examples happens to have that syntax (like [0,1,[foo, bar]].
escaping
This solution is most likely the better way to go. Handlebars will not evaluate any templates that are preceded by \. So you can do this:
<title>\{{title}}</title>
and it will render to:
<title>{{title}}</title>
The first suggestion probably wasn't even worth adding, but hey, you never know right?
Related
I am wondering if there's any function that I can create in order to modify the magic tags behaviour.
Ideally, I would like to use a tag like this {#post_content|120} which would go through my custom function and check if there's a | character, then execute the original magic tag, while trimming text down to 120 characters.
But I don't know where to hook in order to filter this content.
I know that I can pass a function name with the magic tag but this isn't really helpful as I need to pass the characters limit parameter which PODS doesn't support.
Also, I can't be creating functions for all my characters limit as I have a lot of places where I need different limits and I would end up using tons of functions and no dynamic solution.
Can I somehow trigger a magic tag with a parameter? Any other thoughts about doing this another way?
Thank you!
I don't think that's possible, {#your_field, your_function} is how it works (the function takes the field value as input) - you could use different function names like trim_120, trim_100 and do the stuff you need in there - I guess it's to create excerpts with different length's although there are other ways to do that e.g use the_content filter for one ...
I've been having problem displaying accented characters (and somtimes apostrophes too) in twig. The site's been recently hosted and I was experiencing none of this on my local server. This problem's been encountered before (there's a question about it here but using raw doesn't work for me). In twig, I have something like this:
{{ entry.textFr|striptags|raw }}
Despite using the raw filter, I can't get those characters to be displayed right (I'm also aware that the raw tag is dangerous but I fully trust the content of the entry entity).
More profoundly, I'm using symfony2 and stfalcon's tinymce bundle, which seems to convert those characters automatically before they are persisted in the db. None of this was the case on my local server.
Does someone have an idea?
Edit
In particular, it seems that 'é', 'è', 'à', 'ê', 'ô', as well as ellipsis and apostrophes are displayed as é, è, à, etc. I've just tried using the replace filter like this |replace({é': 'é', 'è': 'è', 'à': 'à', //ect }) and it seems to work but it's kind of dirty
Edit 2
Nevermind. I just made a twig filter with html_entity_decode. Didn't wanna use that solution but couldn't find anything else.
I had the same problem, and as I see, lots of people are still having it.
After long researches, I solved with Convert Encoding this way:
{{ mytext|striptags|trim|convert_encoding('UTF-8', 'HTML-ENTITIES') }}
("trim" can be omitted for the post purpose, it was just used to have a cleaner string, in my case)
Working with Laravel - October CMS I solved this issue adding a charset metatag in each file, the parent with "head" closure and childrens without.
I can see the technology-independent Tridion Content Delivery Language (TCDL) link has the following parameters, which are pretty well described on SDL Live Content.
type
origin
destination
templateURI
linkAttributes
textOnFail
addAnchor
VariantId
How do we add multiple attribute-value pairs for the linkAttributes? Specifically, what do we use to escape the double quotes as well as separate pairs (e.g. if we need class="someclass" and onclick="someevent").
The separate pairs are just space delimited, like a normal series of attributes. Try XML encoding the value of linkAttributes however. So, " become "e;, etc...
If you are using some Javascript, you might take care of the Javascript quotes too, as in \".
Edit: after I figured out your real question, the answer is a lot simpler:
You should wrap the values inside your linkAttributes in single quotes. Spaces inside linkAttributes are typically handled fine; but if not, escape then with %20.
If you need something more or want something that isn't handled by the standard tcdl:ComponentLink, remember that you can always create your own TCDL tag and and use a TagHandler or TagRenderer (look them up in the docs for examples or search for Jaime's article on TagRenderer) to do precisely what you want.
My original answer was to a question you didn't ask: what is the format for TCDL tags (in general). But the explanation might still be useful to some, so remains below.
I'd suggest having a look at what format the default building blocks (e.g. the Link Resolver TBB in the Default Finish Actions) output and use that as a guide line.
This is what I could quickly get from the transport package of a published page:
<tcdl:Link type="Page" origin="tcm:5-199-64" destination="tcm:5-206-64"
templateURI="tcm:0-0-0" linkAttributes="" textOnFail="true"
addAnchor="" variantId="">Home</tcdl:Link>
<tcdl:ComponentPresentation type="Embedded" componentURI="tcm:5-69"
templateURI="tcm:5-133-32">
<span>
...
One of the things that I know from experience: your entire TCDL tag will have to be on a single line (I wrapped the lines above for readability only). Or at least that is the case if it is used to invoke a REL TagRenderer. Clearly the tcdl:ComponentPresentation tag above will span multiple lines, so that "single line rule" doesn't apply everywhere.
And that is probably the best advice: given the fact that TCDL tags are processed at multiple points in Tridion Publishing, Deployment and Delivery pipeline, I'd stick to the format that the default TBBs output. And from my sample that seems to be: put everything on a single line and wrap the values in (double) quotes.
I want to allow my users to "bulk export" an archive of selected resources, i.e., http://.../resource/1, resource/2, resource/4, ... ,
My thought was "render the HTML of each page to a string and use java.util.zip to create a multifile archive."
My problem then became "how to get the HTML of a page so that I can loop over them?"
I cannot figure out a way to get a JstlView to render to a String, nor can I see a way to set the ServletOutputStream to be a ZipOutputStream.
My last thought is to actually GET the HTML of each of the resources via HTTP. I imagine that will be easy enough to code, but it seems pretty byzantine. Is there a better way? (Perhaps something with RequestDispatcher.forward()? )
Use a SwallowingHttpServletResponse from DWR (or a PageResponseWrapper from Sitemesh) as a parameter to RequestDispatcher.include() and then get the output from that response object.
See my response (no pun intended) to this question.
I want to make an XML document from an HTML one so I can use the XML parsing tools. My problem is that my HTML is not guaranteed to be XHTML nor valid. How can I bypass the exceptions? In this string <p> is not terminated, nor is <br> nor <meta>.
var poorHtml:String = "<html><meta content=\"stuff\" name=\"description\"><p>Hello<br></html>";
var html:XML = new XML(poorHtml);
TypeError: Error #1085: The element type "meta" must be terminated by the matching end-tag "</meta>".
I did some searching and couldn't come up with anything except this doesn't really seem possible, the major issue is how should it correct when the format is not valid.
In the case of browsers, every browser does this based upon it's own rules of what should happen in the case that the closing tag isn't found (put it in wherever it would cause the code to produce a valid XML and subsequently DOM tree, or self terminate the tag, or remove the tag, or for the case that a closing tag was found with no opening how should this be handled, what about unclosed attributes etc.).
Unfortunately I don't know of anything in the specification that explains what should be done in this case, with XHTML just like how flex treats it these are fatal errors and result in no functionality rather than how HTML4 treated it with the quirky and transitional DTD options.
To avoid the error or give better error messaging you can use this:
var poorHtml:String = "<html><meta content=\"stuff\" name=\"description\"><p>Hello<br></html>";
try
{
var html:XML = new XML(poorHtml);
}
catch(e:TypeError)
{
trace("error caught")
}
but it's likely you'll be best off using some sort of server side script to validate the XML or correct the XML before passing it over to the client.
There is probably an implementation of HTML Tidy in just about any language you might happen to be working with. This looks promising for your sitation: http://code.google.com/p/as3htmltidylib/
If you don't want to drag in a whole library (I wouldn't), you could just write your own XML parser that handles errors in whatever way suits you (I'd suggest auto-closing tags until the document makes sense again, ignoring end tags with no start tags, maybe un-closing certain special tags such as "body" and "html"). This has the added advantage that you can optimize it for whatever jobs you need it for, i.e. by storing a list of all elements with the attribute "href" as you come to them.
You could try to pass your HTML through HTML Tidy on the server before loading it. I believe that HTML Tidy does a good job at cleaning up broken HTML.