tpr Posted May 1, 2016 Share Posted May 1, 2016 (edited) 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. http://modules.processwire.com/modules/template-latte-replace/ https://github.com/rolandtoth/TemplateLatteReplace Edited December 7, 2016 by owzim added Latte link 11 Link to comment Share on other sites More sharing options...
peterfoeng Posted May 2, 2016 Share Posted May 2, 2016 Been waiting for you to release this as I personally use Latte in one of the project. Thank you! Link to comment Share on other sites More sharing options...
tpr Posted May 2, 2016 Author Share Posted May 2, 2016 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). 2 Link to comment Share on other sites More sharing options...
Sergio Posted May 11, 2016 Share Posted May 11, 2016 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 More sharing options...
tpr Posted May 12, 2016 Author Share Posted May 12, 2016 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). 1 Link to comment Share on other sites More sharing options...
Sergio Posted May 12, 2016 Share Posted May 12, 2016 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! 2 Link to comment Share on other sites More sharing options...
tpr Posted May 12, 2016 Author Share Posted May 12, 2016 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 1 Link to comment Share on other sites More sharing options...
Sergio Posted May 12, 2016 Share Posted May 12, 2016 I checked the underscores multiple times, I really don't know what happened. I didn't try using plural yet, I'll let you know if I bump on any problem. Many thanks! Link to comment Share on other sites More sharing options...
tpr Posted May 18, 2016 Author Share Posted May 18, 2016 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 More sharing options...
tpr Posted May 18, 2016 Author Share Posted May 18, 2016 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. 3 Link to comment Share on other sites More sharing options...
tpr Posted May 18, 2016 Author Share Posted May 18, 2016 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. 2 Link to comment Share on other sites More sharing options...
Sergio Posted May 19, 2016 Share Posted May 19, 2016 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 More sharing options...
tpr Posted May 19, 2016 Author Share Posted May 19, 2016 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 2 Link to comment Share on other sites More sharing options...
Sergio Posted May 19, 2016 Share Posted May 19, 2016 Just upgraded it here, everything seems fine! Thank you! 1 Link to comment Share on other sites More sharing options...
Sergio Posted May 21, 2016 Share Posted May 21, 2016 (edited) 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 May 22, 2016 by Sérgio Link to comment Share on other sites More sharing options...
tpr Posted May 21, 2016 Author Share Posted May 21, 2016 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. 1 Link to comment Share on other sites More sharing options...
Sergio Posted May 22, 2016 Share Posted May 22, 2016 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? 1 Link to comment Share on other sites More sharing options...
tpr Posted May 22, 2016 Author Share Posted May 22, 2016 This is my dilemma too. Emogrifier is easy to setup so I'm not sure if it deserves an own module. Of course I'm open to other ideas. Link to comment Share on other sites More sharing options...
tpr Posted May 23, 2016 Author Share Posted May 23, 2016 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. 1 Link to comment Share on other sites More sharing options...
tpr Posted June 8, 2016 Author Share Posted June 8, 2016 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. 5 Link to comment Share on other sites More sharing options...
tpr Posted June 30, 2016 Author Share Posted June 30, 2016 v018 is uploaded with the latest Latte version (2.4.0). There's an official way to set the default layout file so previous workarounds were removed. 2 Link to comment Share on other sites More sharing options...
tpr Posted July 25, 2016 Author Share Posted July 25, 2016 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. 2 Link to comment Share on other sites More sharing options...
Mike Rockett Posted August 29, 2016 Share Posted August 29, 2016 @tpr - Thanks for this module. Have switched over from Twig for a project to test it out, and I dig it. Latte is quite awesome indeed. 1 Link to comment Share on other sites More sharing options...
tpr Posted August 29, 2016 Author Share Posted August 29, 2016 Glad to hear that. Twig felt me a step back after Latte but I haven't really try it in detail. 1 Link to comment Share on other sites More sharing options...
Nicolas Posted October 7, 2016 Share Posted October 7, 2016 Hi, Has anyone succeeded running Hanna Code with Latte template. I'm strugling getting them to work together. Is there a setting to prevent latte to process Hanna Code tags ? Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now