Jump to content
tpr

Module: Template Latte Replace

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

Share this post


Link to post
Share on other sites

Been waiting for you to release this as I personally use Latte in one of the project. Thank you!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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? :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Just upgraded it here, everything seems fine! 

Thank you!

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

  • Like 2

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

@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.

  • Like 1

Share this post


Link to post
Share on other sites

Glad to hear that. Twig felt me a step back after Latte but I haven't really try it in detail.

  • Like 1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By joshua
      This module is (yet another) way for implementing a cookie management solution.
      Of course there are several other possibilities:
      - https://processwire.com/talk/topic/22920-klaro-cookie-consent-manager/
      - https://github.com/webmanufaktur/CookieManagementBanner
      - https://github.com/johannesdachsel/cookiemonster
      - https://www.oiljs.org/
      - ... and so on ...
      In this module you can configure which kind of cookie categories you want to manage:

      You can also enable the support for respecting the Do-Not-Track (DNT) header to don't annoy users, who already decided for all their browsing experience.
      Currently there are four possible cookie groups:
      - Necessary (always enabled)
      - Statistics
      - Marketing
      - External Media
      All groups can be renamed, so feel free to use other cookie group names. I just haven't found a way to implement a "repeater like" field as configurable module field ...
      When you want to load specific scripts ( like Google Analytics, Google Maps, ...) only after the user's content to this specific category of cookies, just use the following script syntax:
      <script type="optin" data-type="text/javascript" data-category="statistics" data-src="/path/to/your/statistic/script.js"></script> <script type="optin" data-type="text/javascript" data-category="marketing" data-src="/path/to/your/mareketing/script.js"></script> <script type="optin" data-type="text/javascript" data-category="external_media" data-src="/path/to/your/external-media/script.js"></script> <script type="optin" data-type="text/javascript" data-category="marketing">console.log("Inline scripts are also working!");</script> The type has to be "optin" to get recognized by PrivacyWire, the data-attributes are giving hints, how the script shall be loaded, if the data-category is within the cookie consents of the user. These scripts are loaded asynchronously after the user made the decision.
      If you want to give the users the possibility to change their consent, you can use the following Textformatter:
      [[privacywire-choose-cookies]] It's planned to add also other Textformatters to opt-out of specific cookie groups or delete the whole consent cookie.
      You can also add a custom link to output the banner again with a link / button with following class:
      <a href="#" class="privacywire-show-options">Show Cookie Options</a> <button class="privacywire-show-options">Show Cookie Options</button> This module is still in development, but we already use it on several production websites.
      You find it here: PrivacyWire Git Repo
      Download as .zip
      I would love to hear your feedback 🙂
      CHANGELOG
      0.0.5 Multi-language support included completely (also in TextFormatter). Added possibility to async load other assets (e.g. <img type="optin" data-category="marketing" data-src="https://via.placeholder.com/300x300">) 0.0.4 Added possibility to add an imprint link to the banner 0.0.3 Multi-language support for module config (still in development) 0.0.2 First release 0.0.1 Early development
    • By MoritzLost
      This is a new module that provides a simple solution to clearing all your cache layers at once, and an extensible interface to perform various cache-related actions.
      The simple motivation behind this module was that I was tired of manually clearing caches in several places after deploying a change on a live site. The basic purpose of this module is a simple Clear all caches link in the Setup menu which clears out all caches, no matter where they hide. You can customize what exactly the module does through it's configuration menu:
      Expire or delete all cache entries in the database, or selectively clear caches by namespace ($cache API) Clear the the template render cache. Clear out specific folders inside your site's cache directory (/site/assets/cache) Refresh version strings for static assets to bust client-side browser caches (this requires some setup, see the full documentation for details). This is the basic function of the module. However, you can also add different cache management action through the API and execute them through the module's interface. For this advanced usage, the module provides:
      An interface to see all available cache actions and execute them. A system log and logging output on the module page to see verify what the module is doing. A CacheControlTools class with utility functions to clear out different caches. An API to add cache actions, execute them programmatically and even modify the default action. Permission management, allowing you granular control over which user roles can execute which actions. The complete documentation can be found in the module's README.
      Beta release
      Note that I consider this a Beta release. Since the module is relatively aggressive in deleting some caches, I would advise you to install in on a test environment before using it on a live site.
      Let me know if you're getting any errors, have trouble using the module or if you have suggestions for improvement!
      In particular, can someone let me know if this module causes any problems with the ProCache module? I don't own or use it, so I can't check. As far as I can tell, ProCache uses a folder inside the cache directory to cache static pages, so my module should be able to clear the ProCache site cache as well, I'd appreciate it if someone can test that for me.
      Future plans
      If there is some interest in this, I plan to expand this to a more general cache management solution. I particular, I would like to add additional cache actions. Some ideas that came to mind:
      Warming up the template render cache for publicly accessible pages. Removing all active user sessions. Let me know if you have more suggestions!
      Links
      https://github.com/MoritzLost/ProcessCacheControl ProcessCacheControl in the Module directory

    • By David Karich
      Admin Page Tree Multiple Sorting
      ClassName: ProcessPageListMultipleSorting
      Extend the ordinary sort of children of a template in the admin page tree with multiple properties. For each template, you can define your own rule. Write each template (template-name) in a row, followed by a colon and then the additional field names for sorting.
      Example: All children of the template "blog" to be sorted in descending order according to the date of creation, then descending by modification date, and then by title. Type:
      blog: -created, -modified, title  Installation
      Copy the files for this module to /site/modules/ProcessPageListMultipleSorting/ In admin: Modules > Check for new modules. Install Module "Admin Page Tree Multible Sorting". Alternative in ProcessWire 2.4+
      Login to ProcessWire backend and go to Modules Click tab "New" and enter Module Class Name: "ProcessPageListMultipleSorting" Click "Download and Install"   Compatibility   I have currently tested the module only under PW 2.6+, but think that it works on older versions too. Maybe someone can give a feedback.     Download   PW-Repo: http://modules.processwire.com/modules/process-page-list-multiple-sorting/ GitHub: https://github.com/FlipZoomMedia/Processwire-ProcessPageListMultipleSorting     I hope someone can use the module. Have fun and best regards, David
    • By dimitrios
      Hello,
      this module can publish content of a Processwire page on a Facebook page, triggered by saving the Processwire page.
      To set it up, configure the module with a Facebook app ID, secret and a Page ID. Following is additional configuration on Facebook for developers:
      Minimum Required Facebook App configuration:
      on Settings -> Basics, provide the App Domains, provide the Site URL, on Settings -> Advanced, set the API version (has been tested up to v3.3), add Product: Facebook Login, on Facebook Login -> Settings, set Client OAuth Login: Yes, set Web OAuth Login: Yes, set Enforce HTTPS: Yes, add "https://www.example.com/processwire/page/" to field Valid OAuth Redirect URIs. This module is configurable as follows:
      Templates: posts can take place only for pages with the defined templates. On/Off switch: specify a checkbox field that will not allow the post if checked. Specify a message and/or an image for the post.
      Usage
      edit the desired PW page and save; it will post right after the initial Facebook log in and permission granting. After that, an access token is kept.
       
      Download
      PW module directory: http://modules.processwire.com/modules/auto-fb-post/ Github: https://github.com/kastrind/AutoFbPost   Note: Facebook SDK for PHP is utilized.


×
×
  • Create New...