Jump to content

Module: Template Latte Replace


tpr
 Share

Recommended Posts

On 4/19/2017 at 11:14 AM, tpr said:

Just upgraded the module to v0.4.3. There's a new option to disable the "noescape" filter, and a workaround for translations on a non-multilanguage site.

The latter is a simple solution for a situation when you copy over a latte file from another project full with translations but the current project is single-language only. This is perhaps not an issue if you're using full strings for translation keys, e.g "_t('Read more')" instead "_t('read_more_text')" because the original key is returned. But even so you may start a non-English project and there you'll be in trouble :) I was there several times and ended up rewriting strings/translations all over the project, now they can remain in place and I have to add translations at one file (which you can copy over).

 

Great additions, @tpr !! The translation workaround can be also useful when you're creating a site that initially isn't multilanguage, but will be in the future, so you can add the translations strings even if the backend doesn't have the multilanguage modules setup yet.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Hey @tpr,

Thanks for the module. Switched over from Twig and really enjoying the custom filters, makes things much easier.

Just wondering about embedding a Form Builder form. As Form Builder creates a form-builder.php template which gets called via Ajax when embedded in another template, I am running into issues for obvious reasons (no .latte view file etc.).

I know there are workarounds and different form builder options, but for this use case I feel like am missing something, like the ability to bypass the view or turn latte off on a template basis. 

Thanks again.

  • Like 1
Link to comment
Share on other sites

Glad to hear that, thanks for sharing :)

Unfortunately I don't have From Builder so I'm afraid I can't tell you anything smart. Does this php file render the form html? How about using something like this? (in basic-page.php or other 'controller file', or in /site/ready.php if you have checked 'Use latte extension ... ' in module config):

$view->form = wireRenderFile('path_to/form-builder.php');

Then in .latte file use '{$form|noescape}'.

Perhaps if you could send me the contents of the php file in PM I could tell something smarter :)

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

In v048 there's a new filter called group. This allows creating groups from a PageArray based on a page field value. I will probably further polish it later when I will use it in more projects but I think it's a nice time saver.

{foreach ($page->children()->sort('sort')|group:'section') as $group}
...

The filter just saved my *** from re-writing a thing to use Repeaters instead using child pages. It was much easier and fun to create the filter :) 

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

Latest version is 0.5.1, which contiains Latte 3.0.0-dev, which means the module requires PHP 7 from now on.

A new savetemp filter was added in v0.5 that allows saving temporary data. This can be handy eg. when creating an image variation in a src attribute, and would like to add a width/height attribute with the resized image data.

Before savetemp you had to save the resized image to a variable first and use this variable in src/width/height attributes, now you can do it much simpler. You could use the "iff" or "setvar" macros too but savetemp is only a matter of adding "|savetemp" where you need it (even inside a filter chain).

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

In the latest v0.5.5 there's a new optionchecked filter. I usually have a tough time when I have to check whether a Select options field has a given option checked/selected, so this filter is about simplify this. I've decided to use a "dot notation" because it makes things easier to read imo.

The filter accepts the id, value and title too:

// 3=show_header|Show header

{if ($page|optionchecked:'page_options.3')} ... {/if}

{if ($page|optionchecked:'page_options.show_header')} ... {/if}

{if ($page|optionchecked:'page_options.Show header')} ... {/if}

 

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

Is there no easy way to change the delimiters? Having a hard time with Vue in my template where it’s conflicting inside class bindings. Turning the syntax off is obviously not a good solution...

i’ve tried using getParser()->setDelimiters(left, right), but it must be too late in the lifecycle for it to be effective, because it still says the macro is unknown.

Edit: Got it working…

$view->latte->getParser()->syntaxes['latte'] = ["\{\@(?![\s'\"{}])", '\}'];

 

Edited by Mike Rockett
working :-)
Link to comment
Share on other sites

  • 2 months later...
On 1/8/2018 at 2:38 PM, Mike Rockett said:

Is there no easy way to change the delimiters?

Sorry @Mike Rockett I completely missed your post. Was your goal to use another syntax instead of "{}"? If so, latte supports syntax switching but I guess you already know that:

https://latte.nette.org/en/macros#toc-syntax-switching

So you can simply add this:

<script n:syntax="double">
    var obj = {var: 123};
</script>

Their docs says there are "asp", "python" syntaxes but those were removed long ago (just found out myself too though I've noticed it earlier that didn't work).

However there's a trick: leave spaces between the delimiters and you're good to go. So in Vue use "{{ myVar|myFilter }}".

  • Like 1
Link to comment
Share on other sites

There are two new macros in v058: ifstatus and ifloggedin.

The former allows checking page status easily and it's quite powerful:

{ifstatus $servicesPage, array('!hidden', '!locked')}
    <p>The services page is not hidden nor locked.</p>
{else}
    <p>The services page is locked or hidden.</p>
{/ifstatus}

($servicesPage is a Page object but it would work with only supplying its ID)

Sometimes I need such checks but it was inconvenient to do such checks in the view file, eg like this:

{if !$servicesPage->is(\ProcessWire\Page::statusUnpublished)}

Macro ifloggedin doesn't need too much explanation I guess :) 

Link to comment
Share on other sites

On 05/04/2018 at 9:47 PM, tpr said:

Sorry @Mike Rockett I completely missed your post. Was your goal to use another syntax instead of "{}"? If so, latte supports syntax switching but I guess you already know that:

https://latte.nette.org/en/macros#toc-syntax-switching

So you can simply add this:


<script n:syntax="double">
    var obj = {var: 123};
</script>

Their docs says there are "asp", "python" syntaxes but those were removed long ago (just found out myself too though I've noticed it earlier that didn't work).

However there's a trick: leave spaces between the delimiters and you're good to go. So in Vue use "{{ myVar|myFilter }}".

No problem, and thanks. I'm going to stick with my original solution as I need it to only be set once... This also gives a level of customisation, which I like.

?

Link to comment
Share on other sites

  • 3 weeks later...

I've added a new filter called replacesubs that allows adding placeholders in format ((name)) to fields, and then use an array or a textarea content to fill them with values.

You can apply it to a field content and all available placeholders will be replaced. Unlike getlines you can add new placeholders and data sources at any time, no need to touch the code. Plus you / your client can add HTML and formatting to them in the CKEditor.

  • Like 1
Link to comment
Share on other sites

  • 8 months later...

I've created a new documentation page with docsify. I was about to ship standalone markdown files with the module so they could be versioned and read through GitHub (or somehow else). Then I looked at @adrian's docs for Tracy and I liked it, especially the search feature, so I decided to go that route.

The next release will contain a major rewrite for filters and macros. They contribute largely to the module's robustness (imo) and I wanted these functions to be available separately too. So there's a new "PWHelpers" class (name is not final) that contains methods that filters and macros are using. This means you can include this class in your non-latte PW projects too and use them if you wish.

Another motivation was to make these functions testable which was not possible without this change. I wrote tests for almost every filter and macro. I spent significant time to it (testing is fun :)) and have fixed many bugs. Of course this doesn't mean they are bug-free but they are much safer to use and when a new feature is added I can easily check if everything works as expected (using ProcessNetteTester btw). 

I was thinking about making this class a separate module but I kept it inside TemplateLatteReplace for now, but I can change this later if there's a need.

  • Like 4
Link to comment
Share on other sites

Thanks for this and all your other work on the module. I'm in the process of converting sites from Clipper (MODX Evolution fork), and find that Latte has just the right level of intelligent templating for me. Together with the Processwire API, it means I've been able to replicate pages without constant hopping in and out of PHP and, so far, without having to write a single database query.

KP

  • Like 2
Link to comment
Share on other sites

Nice to hear that, thanks. As I wrote the additional filters will be modified a lot in the next release so if you use them a lot then updating will be harder. I try to release it in about two weeks but as always, time is not on my side ?

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Hey @tpr, I think I've found a bug.

One of my pages was returning a black page and no error on debug/tracy. Data was passing fine.

After 10 min trying several things here, like removing the template php file to test the error etc and nothing happened. The file was named pdf_report_pdf.latte and it was loaded by another template via viewFile method. 

I then went back to the module config and remove a "pdf" (just "pdf) from the list of templates to ignore as I have another template called pdf.php and voilà, the error was gone. So, I'm guessing you do a regex on the ignore list entries? Was that the problem?

Link to comment
Share on other sites

4 hours ago, tpr said:

I think the problem is the strpos function. It will return true for "pdf" in "report_pdf" for instance. But we need to return true only if it's an exact match. So, what about this:

foreach ($ignoredTemplates as $t) {
  if (preg_match('/\b'.trim($t).'\b/',$templateFile->filename)) {              
  	return;
  }
}

I tested it here and it works.

Link to comment
Share on other sites

On 2/12/2019 at 12:15 PM, Sergio said:

I think the problem is the strpos function. It will return true for "pdf" in "report_pdf" for instance. But we need to return true only if it's an exact match. So, what about this:


foreach ($ignoredTemplates as $t) {
  if (preg_match('/\b'.trim($t).'\b/',$templateFile->filename)) {              
  	return;
  }
}

I tested it here and it works.

Hey, @tpr did you have the time to check out this? Thank you!

Link to comment
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
 Share

×
×
  • Create New...