include file without swig parsing it as a template - swig-template

I would like to include a file in a swig template without swig parsing it as a swig template. Something like raw tag for files.
Using swig-highlight I would like to get this working.
{% highlight 'html' %}
{% import 'some-template-file.html' %}
{% endhighlight %}
Problem is 'some-template-file.html' is a Jekyll template and throws errors in swig.

You can try using the raw tag. This ignores any inside swig syntax and should, presumably, ignore any other syntax as well.
// inside (e.g.) index.html
{% import 'some-template-file.html' %}`
// inside some-template-file.html
{% raw %} {{ testVar }} {% endraw %}
// results in "{{ testVar }}"

Related

Is it possible to share HubL macros across modules and templates?

In the HubSpot CMS, you can create a macro like so:
{% macro render_section(classes, background_style) %}
<div class="mosaic-section {{classes}}" {{background_style}}>
{{ caller() }}
</div>
{% endmacro %}
Is it possible to share this macro across different modules and templates? Or do you have to repeat the macro everywhere you need to use it?
Yes, you can share macros across modules but only the modules that are in the same scope as an imported HTML partial/snippet that contains the macros you've created.
According to the HubL docs for using the {% import %} tag (found here), macros can be imported from user created HTML partials/snippets. For example, if you were to create the following HTML partial called macros.html:
{% macro render_section(classes, background_style='', data='') %}
<div class="mosaic-section {{classes}}" {{background_style}} {{data}}>
{{ caller() }}
</div>
{% endmacro %}
You would then import macros.html into a template, for example, called homepage.html with the following HubL/HTML code:
<!doctype html>
<head>
{# standard HubL header code goes here %}
</head>
<body class="site-page two-column {{ builtin_body_classes }}" style="">
{% import 'path/to/macros.html as module_macros %}
<!--- more HubL/HTML code .... -->
{{ standard_footer_includes }}
</body>
</html>
And as result, all modules added to the homepage.html coded template are now in the same scope as the imported macros and as such said modules can now utilize the macros.
If you'd like to import a single macro from a HTML partial that contains multiple macros you can use the {% from %} tag (found here) and perform the following:
{% from 'path/to/macros.html' import render_section %}
Now the macro render_section() is available to all proceeding modules in the coded template.
NOTE:
Unfortunately, I have yet to find a way to use/import macros "globally" into a drag-and-drop template -- at least without having to use an embed HubL module which can add weird spacing issues in the generated HTML markup that you may need to resolve using CSS.
Hope this answer was sufficient and helps resolve the problem you've posed.

zendesk - semantic template array accessor

Is there a way for me to access a particular element of an array using Zendesk semantic templating. https://connect.zendesk.com/hc/en-us/articles/115010914668-Using-the-email-editor#topic_h2z_hn5_r1b
Normally you would go through each element of the array
{% for image in 'images' | EventProperty %}
<img src="{{ image.src }}">
{% endfor %}
But for my current use case I only need the first element. I wanna do something similar to the following:
<img src="{{ images[0].src }}">
The syntax looks like Liquid Markup, and I know they use Liquid in other areas of the product. Have you tried the Liquid Filter 'first'?
{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}
{{ my_array.first }}

Ruby dynamic binding variable as object

Consider the following loop
{% for product in collections.settings[collection_setting].products %}
{{ settings[collection_setting] }} - {{ product.title }}
{% endfor %}
Here settings[collection_setting] is equal to green but this does not work. If I have this static it works example
{% for product in collections.green.products %}
{{ settings[collection_setting] }} - {{ product.title }}
{% endfor %}
Can anyone please explain why this happens and how to solve this.
Answer
Sorry for wasting your time. This helps me to work.
Working
{% for product in collections[settings[collection_setting]].products %}
{% endfor %}
collection_setting seems to be a ruby variable. Hence to have the liquid template engine evaluate this, you'll need to have it in double braces ({{collection_setting}}).
Where you have this:
{% for product in collections.settings[collection_setting].products %}
...are you sure you don't mean this?
{% for product in collections[collection_setting].products %}
Your non-working example has a .settings where your working example does not.

Extending erlydtl

How is it possible to extend "erlydtl"?
I really like django templates, and the way the template language can be extended. For example, I like the extensions such as "sekizai".
It is possible to have custom tag modules for erlydtl. But how do I add support for more complex tags such as provided by sekizai?
In django, using sekizai I can do following (taken from sekizai documentation).
{% render_block "css" %}
And add following to add to the above block
{% addtoblock "css" %}
<link href="/media/css/stylesheet.css" media="screen" rel="stylesheet" type="text/css" />
{% endaddtoblock %}
And this will add the contents at the place where {% render_block %} is called.
You need to write your own module and define functions that are called and provide the data for your template tags.
Example:
File perc_filter.erl:
-module(perc_filter).
-export([percentage/2]).
percentage(Input, Whole) when is_integer(Input), is_integer(Whole) ->
[Result] = io_lib:format("~.2f", [Input / Whole * 100]),
Result.
In template:
{{ x|percentage:1000 }}

Django templates problem — {% if object|length > 4 %} raises TemplateDoesNotExist: 500.html

I have the following in my template.
{% block content %}
{% for album in albumsList %}
{% if fotosList %}
<div class="photoalbum-wrapper">
<h3>{{ album.title }}</h3>
<ul class="photoalbum">
{% for foto in fotosList %}<li>item</li>{% endfor %}
</ul>
{% if fotosList|length > 4 %}
больше <span>▼</span>
{% endif %}
</div>
{% endif %}
{% endfor %}
{% endblock %}
And it raises TemplateDoesNotExist: 500.html.
If i write simple {{ fotoList|length }} it works okay.
This is a very old question. Since then, newer versions of Django support operators in if-statement out of the box, so the following code will work just fine:
{% if fotosList|length > 4 %}
{% endif %}
Use fotosList.count instead of fotosList|length. you will get desired result.
FYI if tags with the operators ==, !=, <, >, <=, >= are supported now in the development version of Django.
{% if fotosList|length > 4 %} is not a valid tag; you can't use greater than/less than operators in the Django if tag. (You can use operators in the latest development release, but I'm assuming you're not using the latest version from Django's SVN repository.)
The reason you get the TemplateDoesNotExist error is because Django is throwing a 500 Internal Server Error (due to the invalid tag), but you haven't supplied a 500.html error template, as noted here.

Resources