How do I create a jinja2 filter and use it within pylons? - pylons

Am going around the houses trying to find a way to implement a simple filter.
I want to create the equivalent of some Smarty "tags" to make porting easier, notably
{mail_to} http://www.smarty.net/manual/en/language.function.mailto.php
I seem to be going around in circles between the jinga2 docs
http://jinja.pocoo.org/2/documentation/extensions#module-jinja2.ext
and the webhelpers
http://pylonshq.com/docs/en/0.9.7/modules/templating/
What I'm expecting to write is something like
{% mail_to user=c.user.email encode='hex' %}
Cant figure out how to piece it all together, ie location of lib and how to load for usage.
tia

Write you extension and put it into lib/extensions.py
To load you extension into environment do in config/environment.py:
from MYAPP.lib import extensions
config['pylons.app_globals'].jinja2_env = Environment(loader=ChoiceLoader(
[FileSystemLoader(path) for path in paths['templates']]),
extensions=[extensions.YOU_EXTENSION_MAIL_TO_CLASS]))
# If you extension use some options, you can init it
config['pylons.app_globals'].jinja2_env.mail_to_smtp_host = 'some_host'
After in yours templates simply call {% mail_to arg1, arg2 %}

Related

Insert fragment from one module to template in another - thymeleaf

I need some help. I have let say three maven modules: moduleMain, moduleCore, module2, module3. I have in moduleCore simple html file, eg. Share.html which has some html code inside. And now I want to use Share.html in my module1 and module2 in Thymeleaf templates How to bite it?
I search in documentation, but unfortunately, I found only include, insert which examples are only when we have all in one place. This not works for me.
Thymeleaf version: 3.0.9.RELEASE, I use them with Spring.
Solution
I found solution, only need to add all HTML files to default folder: web-inf/templates and it works.

Why is there a sometimes a trailing underscore in the Pipeline #Library() syntax

Why does the #Library() call require the trailing _ (underscore) character at least in some cases? e.g. #Library('foobar-library#foobranch') _
That seems completely superfluous to me but maybe there's a good explanation?.
Based on this Jenkins blog
After configuring Jenkins so that it can find the shared library repository, we can load the shared library into our pipeline using the #Library('') annotation.
Since Annotations are designed to annotate something that follows them, we need to either include a specific import statement, or, if we want to include everything, we can use an underscore character as a placeholder.

Twig, ZF2 and PoEdit

Using ZfcTwig for ZF2 and twig-gettext-extractor, I still cannot extract messages for translation from twig-files by poedit. I works if I used the formal twig keyword for translation {% trans(MY_TEXT) %} but not for the in-built view helper translate. {{ translate(MY_Text) }} does the translation but poedit is just ignoring it. For new twig files, I want as usual let poedit do the job....
Any ideas for a solution?
Maybe you need to edit catalog properties keywords to be visible for translations. Open PoEdit, go to Catalog -> Properties -> Sources keywords and add another keyword "translate". Here I attach an screenshot.
The problem is that the extractor you are using is just caching the files and running them through xgettext to extract calls to trans/translate/_/.. (keywords as suggested by Conti. Alas ZfcTwig will crosscompile calls to ZF2 plugins into plugin('translate')->__invoke('Your Text to be translated'). You could of course now add __invoke as a new keyword in poedit or whatever you favorite gui for using xgettext is, but it will now find all calls to all view helpers not just those to translate.
I ran into this problem myself and I have not come up with a satisfying solution. The Twig Gettext Extension looks promising in terms of writing your own customized POT-File generator that will handle translate and translatePlural view helper calls. Using the extension as is will bypass all translation from ZF2. ZF2 parses .mo files into an internal structure rather than using the php-gettext mod.
All in all I gave up on automated po(t) file generation for ZF2+ZfcTwig for now and am back to phpArray.

Gettext throws "warning: unterminated string" when parsing twig template files

This problem has been bothering me for a while, but I would like to fix it once and for all.
I am using the twig template engine in my PHP application. I am using the latest version of Poedit (which uses xgettext 0.18.1) to parse my files for translation strings.
I have set up Poedit to work with twig using these instructions.
Everything works well, but the problem is that when I update my Poedit catalogue, I get errors like this:
1:24:45 PM: somefile.twig:5: warning: unterminated string
1:24:45 PM: somefile.twig:10: warning: unterminated string
This is the dialog:
And this is the file in question:
{% extends somevar ? 'one.twig' : 'two.twig' %}
{% block blah %}
Blah blah
{% endblock %}
{% block blah2%}
<div id="some-id" class="some-class">
some content
</div>
{% endblock %}
However, if I click OK in the error dialog, everything seems to be fine and the strings from the twig template are then loaded into the catalogue.
I know there is a gettext-extractor for twig that has been released recently to pick out the translation strings. However, there are some possible issues:
I am not using the symfony 2 framework and am not using the intl extension for twig (we have built our own to suit our purposes).
We use a different character for our gettext strings {{ t('some string') }}
We would prefer not to have to introduce more external dependencies unless we really have too, otherwise someone trying to get the strings would need to set up twig and the gettext extractor.
Is there a flag I can pass to xgettext to solve this problem? The parsing works fine. I just prefer that the error to not be thrown by gettext.
Change Setting like this:
Go to Prefernces -> Parsers
Select PHP -> Edit
List of Extentions .... -> .php,.phtml
Insert this "xgettext --language=PHP --force-po -o %o %C %K %F" into Parser Command: field.
And also Catalog --> Properties --> Translation Properties TAB --> Charset utf-8
So I continued using that workflow for a few days and found the following problem:
If I have a resource bundle key like so: Test, the settings I was using previously would not pick up the key.
Since I already had a small PHP script in the background to monitor and compile my assets using Assetic, I simply added a few more lines for the twig templates to be compiled into PHP files (http://twig.sensiolabs.org/doc/extensions/i18n.html#extracting-template-strings).
I then remove the Twig parser from POEdit and now, all the template strings are just parsed from PHP files, which works great!
While this solution works, I would still like to find a way to get POEdit and gettext to parse twig files natively. However, if anyone is looking for a solution, this one should work quite well in the meantime.

How to prevent LaTeX from hyphenating words containing a dash?

I would like to globally prevent LaTeX from hyphenating 'Objective-C'. I am aware of the \hyphenation command, but I don't see how I can make use of it. If I pass 'Objective-C' to this command, the dash will be treated as a hint to hyphenate the word there.
One solution I found is wrapping Objective-C into an mbox each time I use it. However, the document I am writing contains this name a lot, and wrapping it into an mbox each time is ugly (as is defining a command and using this over and over again in the source code).
Why is defining a new command ugly? It's how \LaTeX\ defines itself.
\def\ObjectiveC{\mbox{Objective-C}}
Use \nobreakdash. That's what LyX produces when I insert a nonbreakingdash and convert it to tex.
As suggested here, you could define a command like this:
\newcommand\dash{\nobreakdash-\hspace{0pt}}
and use it like this
Consider the $n$\dash dimensional manifold ...
Also, you could use the babel package and use "~ as a protected hyphen. I'm not sure if using babel is advisable when writing in english, though.

Resources