Welcome Guest, Not a member yet? Register   Sign In
Twig help or hindrance
#11

@ivantcholakov

Yeah sorry that was a bad example, my main issue is if you want to add your OWN helper function, how would you go about doing this in twig without having to dynamically add a twig function?

Any ideas?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#12

Configuration: https://github.com/ivantcholakov/starter...r_twig.php
Reply
#13

(This post was last modified: 02-07-2017, 03:39 PM by ignitedcms.)

(02-07-2017, 03:24 PM)ivantcholakov Wrote: Configuration: https://github.com/ivantcholakov/starter...r_twig.php

Interesting...

But what I meant was let's say the end user, makes their own helper function, and they could create any number of them, it could be really simple or very specific/complex.

How do they call that without having to declare a custom twig function in a config file. (assuming they have no knowledge of adding twig functions)

^^That's my issue.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#14

Just a side note, if you plan on using the 'echo shorttag' like <?= $variable ?> be sure you don't want to support PHP<5.4. Prior to PHP5.4 if you disabled php shorttags you disabled the this echo function as well. Since 5.4 it's better seperated, even if you turn off php shorttags (like the 'normal' opening tag <? instead of <?php) you can still use the short echo tag.

To be ontopic, I'm more in line with @skunkbad. I do share the opinion that {{ variable }} looks pretty nice, but less technical staff (designers) still  fuck things up because they dont understand. And a fresh junior developer will need to learn another language to make sense of this, while <?php echo $variable ?> should be clear.
Reply
#15

(This post was last modified: 02-07-2017, 03:46 PM by ignitedcms.)

(02-07-2017, 03:40 PM)Diederik Wrote: Just a side note, if you plan on using the 'echo shorttag' like <?= $variable ?> be sure you don't want to support PHP<5.4. Prior to PHP5.4 if you disabled php shorttags you disabled the this echo function as well. Since 5.4 it's better seperated, even if you turn off php shorttags (like the 'normal' opening tag <? instead of <?php) you can still use the short echo tag.

To be ontopic, I'm more in line with @skunkbad. I do share the opinion that {{ variable }} looks pretty nice, but less technical staff (designers) still  fuck things up because they dont understand. And a fresh junior developer will need to learn another language to make sense of this, while <?php echo $variable ?> should be clear.

Wait I thought CI came with rewrite short tags for servers less than php 5.4 in the config file

Code:
/*
|--------------------------------------------------------------------------
| Rewrite PHP Short Tags
|--------------------------------------------------------------------------
|
| If your PHP installation does not have short tag support enabled CI
| can rewrite the tags on-the-fly, enabling you to utilize that syntax
| in your view files.  Options are TRUE or FALSE (boolean)
|
*/
$config['rewrite_short_tags'] = TRUE;

Also I'm leaning towards a non-twig version purely because it does require an extra learning step as well as my own server doesn't support twig (which is really a lame excuse)
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#16

True, but I was speaking more in general. Plus, that option is by default turned off. You would need to enable it.

It's just one of those things I ran into in my carrier that made me swear off shorthands. Took me a few hours to find out why my code was not working on the production server and fix it.
Reply
#17

(This post was last modified: 02-07-2017, 05:15 PM by ivantcholakov. Edit Reason: a typo )

(02-07-2017, 03:38 PM)iamthwee Wrote:
(02-07-2017, 03:24 PM)ivantcholakov Wrote: Configuration: https://github.com/ivantcholakov/starter...r_twig.php

Interesting...

But what I meant was let's say the end user, makes their own helper function, and they could create any number of them, it could be really simple or very specific/complex.

How do they call that without having to declare a custom twig function in a config file. (assuming they have no knowledge of adding twig functions)

^^That's my issue.

The set of functions I figured at the moment is enough. I also support widgets, so if a user tries to make something more complex, he/she could use them.

Indefinite need for functions tells me that something wrong is going to happen in the views, IMO. Probably logic that does not belong to a view is implemented there. Everything can be calculated in the controller (or a in a subordinate model) and then just to be passed to the view.

I am not a fan of the almost totally "logic-less" engines like Mustache or Handlebars, but still stays the problem to be solved - what logic belongs to a view and what belongs elsewhere. I think, Twig provides the right balance.

Furthermore, you would have automatic escaping by default, which is important. That is more safe. If your end-user makes security mistakes within the PHP-views, I guess he/she would blame your system, not him/her-self.  Smile 

Even if you are not a fan of Twig, maybe a significant number of your end-users might be. Try to check this somehow.

IMO, eventually, implementing an optional template engine within your system would check it conceptually. When I migrated my views, I purged my poor decisions about them and as a result my code got better placed than it was before.
Reply
#18

If if you're building a full blown CMS, there are several considerations to drive your decision, but for me, in general, once I started using Twig a few years ago in my CI apps, I haven't looked back. I reached a point that there isn't anything I can't do inside a template, although it didn't happen over night. It's an easy language/syntax to learn, keeps views super clean ( and almost always fewer lines, less code), and I find it easier to set up templates, load dynamic partials, etc. that with CI views.

We use Twig for every CI project we build. If I can help someone with anything specific, please let me know.

I encourage you to commit to trying it for at least a few days and see if it doesn't click.
Reply
#19

Interesting... I've a few things to mull over but to be honest I am leaning towards a non template solution.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB