Jump to content

Module: Template Latte Replace


tpr
 Share

Recommended Posts

Template Latte Replace

Latte template engine support for ProcessWire

Having wrote about this one for a few times and now here it is. The readme is not complete but should give a solid starting point.

 
Feel free to ask if something's not clear.
 

https://github.com/rolandtoth/TemplateLatteReplace

Edited by owzim
added Latte link
  • Like 11
Link to comment
Share on other sites

Thanks! Keep in mind that it requires PW 3 because of the namespace. There' no specific reason to make it PW 3 only but currently I have no time to rewrite.

Just added two global functions t() and n() to make string translations easier (v0.12).

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Hi @tpr!

First of all, thank you for this module! I decide to use it instead of jade-php on my current project.

The problem is, I can't get the translations on _string.php to work.

I was getting an error when using v0.12 and i just saw that you released to 0.14. I download it, the error is gone but the translation won't appear, only the default text.

I tried directly on the .latte:

{_'text to translate'}

And setting a $view->variable on the controller. 

Any hints? What am I missing?

EDIT: I'm using the latest PW dev

Thanks!

Link to comment
Share on other sites

And setting a $view->variable on the controller

You don't need to set any variable for translation. If you need to translate string in the controller, use "_t('Text to translate').

In .latte file you can use '_', '_t'.

I tried to replicate your issue, here are the steps (tried on 3.015 and 3.017 also):

1. Added "<p>{_'Text to translate'}</p>" into "home.latte" (one underscore). Text appearead in my home page fine, untranslated.

2. Added this to "_strings.php" - mind the double underscore here:

__('Text to translate')
3. In the admin language editor I added the corresponding translation. Prevously I have selected the file "_strings.php" to translate.
etalsnart ot txeT
After that, this translated text appeared on the page. Even for the default language when setting it another value in the Language Translator.

My further bugsolving thoughts are:

  • are you sure the same string is in _strings.php and your .latte file?
  • try clearing Latte cache in module settings

Btw, just realized that _strings.php doesn't need starting php tag and comments but only listing the translations (without semicolon at the end).

  • Like 1
Link to comment
Share on other sites

Well, I installed a fresh copy of 3.0.17 and started all the steps again.

At the first moment, it didn't work, but after I changed the lines on _strings.php to 

  • _x('Text to translate', 'General');

instead of __('Text to translate'), the translations appeared! 

But I tried that on my current project and it didn't work, so I'm trying to figured it out here. I'll keep you posted.

EDIT: It worked on my project as well! I was forgetting to set the context on one string. So everything is good now!

  • Like 2
Link to comment
Share on other sites

Glad you figured it out! It's interesting why it doesn't work without the context for you. Are you sure you haven't messed up the underscores? There is one in latte files, and two in _strings.php. But I guess you checked that enough times :)

Getting string translation to work is a bit tedious at first, but once you get the hang of it it's easy. Using plurals adds another layer of complexity, even if I tried hard to make it as simple as I can :)

  • Like 1
Link to comment
Share on other sites

You were right, "General" context was used even when it shouldn't. Currently I have no idea how to fix this but will try to come up with something. Since the translator gets only the string to translate, there is nothing to check that it was called "_()" or "_t()", not even calling debug_backtrace.

Link to comment
Share on other sites

I got it working, but the "General" context will be gone. I think it's even better this way. Will upload a new version soon, after testing the different variations a bit.

  • Like 3
Link to comment
Share on other sites

v015 is uploaded to GitHub.

Syntax for string translation in .latte files is changed to "{_'text, context, textdomain'}". In textdomain use "__()" if you need no context, and "_x()" if you do.

  • Like 2
Link to comment
Share on other sites

v015 is uploaded to GitHub.

Syntax for string translation in .latte files is changed to "{_'text, context, textdomain'}". In textdomain use "__()" if you need no context, and "_x()" if you do.

So, in my case, as I'm already using _x(), nothing do do, right? :)

Link to comment
Share on other sites

Right. Previously the readme contained a wrong syntax for latte files "_('text')", but it shouldn't need the brackets. So if your syntax is also right it would work fine. Clearing latte cache is recommended though. But you should see at once whether it works or not :)

  • Like 2
Link to comment
Share on other sites

A small contribution to the project readme:

For Sublime Text 3 (should work on 2 too):

  • Install Nette + Latte + Neon package
  • Install Smarty package
  • Set latte extension to open as smarty so you can keep the HTML code autcomplete working

Bonus: install HTML-CSS-JS Prettify package and on its preferences add latte and smarty on allowed file extensions:

"html": {
    "allowed_file_extensions": ["htm", "html", "xhtml", "shtml", "xml", "svg", "latte", "smarty"],

I submitted a pull request editing the readme to add this warning:

HTML Prettify may break lines on strings translations that have more than one word, for instance:
{_'A technical book', 'Books'}

May result in
{_'A 
technical 
book', 'Books'}

so it won't appear translated any more. You'll have to remove the line breaks manually (for now). I'll see if changing something on the package preference does the trick.

Edited by Sérgio
Link to comment
Share on other sites

Great, thanks! Just updated the readme.

I'm thinking about adding Emogrifier to the module. I have only one usecase for inlining CSS (newsletters) but I think it won't hurt having a "$view->setInlineCSS('path') or similar helper at disposal.

  • Like 1
Link to comment
Share on other sites

Great, thanks! Just updated the readme.

I'm thinking about adding Emogrifier to the module. I have only one usecase for inlining CSS (newsletters) but I think it won't hurt having a "$view->setInlineCSS('path') or similar helper at disposal.

I think Emogrifier can be handy but should it be part of the module? Why not a different module, so it can work without latte too?

  • Like 1
Link to comment
Share on other sites

Just for the info: latest Nette/Latte plugin for PhpStorm works fine - syntax highlight, autocomplete and such thing works like charm. I will update the module readme later.

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

v016 was uploaded today to GitHub:

  • enable setting template latte file with/without extension ($view->viewFile = 'basic-page' and $view->viewFile = 'basic-page.latte')
     
  • $view->json_encode returns JSON encoded string/markup. Possible values are true and PHP's json_encode options (eg. JSON_PRETTY_PRINT).
     
  • if "viewFile" begins with "//", $config->paths->templates and viewDir is not prepended to the view path

These updates make it easier to get Ajax results, and allow placing latte files outside templates/views directory. The latter was also possible earlier using relative paths ("../"), but now it's more flexible. Getting json output was only possible using wireRenderFile() and applying json_encode() manually, which was less convenient.

  • Like 5
Link to comment
Share on other sites

  • 4 weeks later...

Here is an example for an undocumented feature: adding macros.

Put this in 'site/ready.php' (or where you set global parameters for Latte, eg. _init.php or maybe _main.php):

$view->_addMacro['custom iff'] = array(
    'iff',	// macro name
    'if (isset(%node.word) && %node.word) { $x = %node.word;', // PHP code replacing the opening brace
    '}' // // code replacing the closing brace
);

This will add an "iff" macro which eliminates the need of duplicating "$page->title" in the example below:

<h1 n:if="$page->title">{$page->title}</h1>

So instead of writing the above you can use $x:

<h1 n:iff="$page->title">{$x}</h1>

The "iff" macro also uses "isset()" so no worries if you throw in a property that doesn't exists, it won't generate an error.

Using "$x" is arbitrary, you can set any variable name if you wish.

Of course, you can use the alternative syntax too:

{iff $page->title}
<h1>{$x}</h1>
{/iff}

See more in the official docs here on creating macros.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...