Jump to content


Photo

Template Engine

template engine twig liquid markup templates mustache handlebars wp php simple syntax

  • Please log in to reply
74 replies to this topic

#1 Natetronn

Natetronn

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 9

  • LocationSan Diego

Posted 10 June 2012 - 11:12 AM

Hi,

I'm curious if anyone has implemented some sort of template ala twig, liquid, mustache, handlebars etc. so on and so forth?

I noticed a blog post about blog themes which needed to be easier to work with than WP and using a simplified template syntax would definitely help for non-php-ers.

The requirement is that it must be as easy (or easier) than WordPress, both to install and use



#2 diogo

diogo

    Hero Member

  • Moderators
  • 2,007 posts
  • 1081

  • LocationPorto, Portugal

Posted 10 June 2012 - 12:01 PM

Hi. and welcome to the forum!

There's not really a point on implement something like this in Processwire. PW use of PHP is very well thought to make it easy to anyone with no PHP skills to learn and use. Have a look at this page where Ryan explains this design decision http://processwire.c...why-php-syntax/.

The requirement is that it must be as easy (or easier) than WordPress, both to install and use

The context of this quote is a blog ready-to-use PW install. Ryan meant that this profile should be as easy for non-programmers to start using immediately.

#3 Natetronn

Natetronn

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 9

  • LocationSan Diego

Posted 10 June 2012 - 12:16 PM

Hi diogo,

Thanks for the welcoming!

I forgot this topic could possibly lead to a "evangelical" debate :/

With that said, I'm glad PW has taken a stance on this one way or another. I haven't taken the time to dive into the PW templates yet so, yes, Ryan's points may very well be sound and your comment about " it easy for anyone with no PHP skills to learn and use" might also be as well.

I will say that Ryan's article touches on Smarty and to be fair that's a template engine of yesterday as far as I'm concerned unless something has changed in Smarty 3 (also debatable I'm sure.)

#4 diogo

diogo

    Hero Member

  • Moderators
  • 2,007 posts
  • 1081

  • LocationPorto, Portugal

Posted 10 June 2012 - 01:00 PM

Not evangelical at all, as I'm not even an experienced programmer. I only learned PHP in the past few months, and I can say it was mostly because of the way PW is thought out. As a Designer I never thought I would be writing some rather advanced PHP so soon, but because PW uses PHP instead of a templating engine, and because it's designed to make it so easy for anyone to achieve basic tasks, you just feel encouraged to go further and further without any constraints.

Really, just start building a website with PW without thinking of external tools, you don't even need to install any of the excellent custom modules that are already available, and you will see how easy it is and how powerful it can get ;)

#5 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 117

  • LocationEutin, Germany

Posted 10 June 2012 - 01:33 PM

Really, just start building a website with PW without thinking of external tools, you don't even need to install any of the excellent custom modules that are already available, and you will see how easy it is and how powerful it can get ;)


I second that. Since I'm involved in another open source project which actually uses Smarty, I have been working with Smarty for some years now. I still like it because it saves me from having to do "actual" coding – Process Wire's API does just the same, but offers more flexibility and possibilities.

#6 Natetronn

Natetronn

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 9

  • LocationSan Diego

Posted 10 June 2012 - 02:00 PM

I was making a suggestion to make things easier for folks with less php experience and easier than WP at the same time.

This might already be the case with PW PHP syntax as it currently stands so, my suggestion maybe a bit off base.

#7 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 10 June 2012 - 02:10 PM

Nate, welcome to the forums and thanks for the suggestion. It is something that pops up every now and then and certainly something to think about. I think only thing that makes template languages more newbie friendly is that those fail silently. Forgot ending ?> from <?php and whole site won't load. This single thing might be big obstacle for someone who is just starting.

What comes to echoing variables and making simple if statements or foreach loops - I don't think that any template language is much easier than PHP. I have only used EE tags, Smarty and tried Laravel's Blade, so I don't know too much of those.

#8 Natetronn

Natetronn

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 9

  • LocationSan Diego

Posted 10 June 2012 - 02:10 PM

I do have an off topic yet possibly related question about PHP short tags. And excuse me if I'm being naive though, should short tags be a part of the requirements list?

#9 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 10 June 2012 - 02:11 PM

I don't think PW uses short tags internally, so using those is purely optional. I might be wrong here though...

#10 teppo

teppo

    Sr. Member

  • Members
  • PipPipPipPip
  • 388 posts
  • 385

  • LocationFinland

Posted 10 June 2012 - 03:27 PM

I don't think PW uses short tags internally, so using those is purely optional. I might be wrong here though...


Happened to have a PW site open, so I did some grepping (is that a word? should be..) and seems to me that you're absolutely right. No short tags found from core files.

@Natetronn, I'm trying to avoid getting all evangelical here, so I'm keeping this as short as possible: IMHO learning enough PHP to write templates effectively is much easier task than learning enough of most templating languages out there to do the same. PHP has very good documentation (php.net), wide user base (try googling pretty much any problem you're having and add "stackoverflow" to your query, it's rare to find a question someone hasn't already answered there) and there are many fantastic tutorials floating around. "It's easier than you think."

That said, I don't have that much experience with templating languages -- mostly I've used Twig and several in-company solutions, so I'm not much of an expert on this subject. All I can say is that after years of learning, fighting and cursing various templating languages and their shortcomings, being able to write templates in pure PHP is a bliss for me :)

#11 Natetronn

Natetronn

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 9

  • LocationSan Diego

Posted 10 June 2012 - 03:45 PM

Hey Teppo,

Yeah, I feel it's really a "what ever floats your boat" kind of debate or if you prefer a "left brain vs right brain" kind of thing if that makes sense?

My own experience has been that using template engines has been great though, I'm to the point where I feel they are some what of a "crutch." I mostly say that because I actually want to learn more advanced PHP (or similar language) now more then ever (before) so, my point of view has changed over time in other words.

Thanks for all the feedback by the way.

#12 Natetronn

Natetronn

    Jr. Member

  • Members
  • PipPip
  • 19 posts
  • 9

  • LocationSan Diego

Posted 10 June 2012 - 03:52 PM

By the way, I know my last reply probably sounded semi-hypocritical when compared to the start of this topic though, I started using template engines (smarty) back in I think 2006 give or take so, what I call a crutch now definitely helped get me get to this point and there may still be value in that all the same.

#13 diogo

diogo

    Hero Member

  • Moderators
  • 2,007 posts
  • 1081

  • LocationPorto, Portugal

Posted 11 June 2012 - 09:11 AM

I think only thing that makes template languages more newbie friendly is that those fail silently. Forgot ending ?> from <?php and whole site won't load. This single thing might be big obstacle for someone who is just starting.


I really don't see that much difference between

{% for item in navigation %}
   <li><a href="{{ item.url }}">{{ item.title }}</a></li>
{% endfor %}

and

<?php foreach($navigation as $item): ?>
   <li><a href="<?= $item->url ?>"><?= $item->title ?></a></li>
<?php endforeach; ?>

If you write it like this, you can forget the }} as much as the ?>
Of course the Twig code looks a little more friendly at the first sight, but the advantages of getting used to writting PHP are worth it.

Edited by diogo, 11 June 2012 - 09:15 AM.


#14 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 11 June 2012 - 09:14 AM

Syntax is easy in both. What I meant that if you make error in php it kills the page. That is pretty extreme for live editing fellow who is just starting.

#15 diogo

diogo

    Hero Member

  • Moderators
  • 2,007 posts
  • 1081

  • LocationPorto, Portugal

Posted 11 June 2012 - 09:20 AM

Hm, I don't really think it's extreme. When someone decided to install the software on a server, had to mess around with permissions and so... he has to be ready to learn the hard things of life ;)

#16 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • LocationAtlanta, GA

Posted 11 June 2012 - 02:40 PM

I've always thought that the "template engines make it easy for non-programmers" was a myth, as it really just comes down to semantics and what characters you think are easier to type. But the reality is that template engines give you something like a jailed environment, and that increases the comfort level of some people. The prospect of limitation becomes an asset in that context. It means it's going to be harder to break things, and there is going to be a ceiling on what can be learned. I don't ever want to be in a "jailed" environment with a low ceiling, but also kind of understand the appeal to someone that may have never stepped beyond the likes of EE (which has come crossover with our audience). As we work to broaden the audience for ProcessWire, an alternate template engine for those that desire limitation here may help us to capture new audiences. There is also just the word "PHP", which scares off some, regardless of what is real or what we do.

ProcessWire is always going to be an PHP API-driven environment at it's core, but I'm not opposed to adding on template engine(s) as an option for those that want them, in the future. It's something that's not at the top of the list on priorities, but it is something we'd like to eventually offer. They are a little more tricky to implement in PW vs. a system that is built purely for tags. The reason for this is that ProcessWire templates are executed directly by PHP rather than by ProcessWire itself. ProcessWire just hands off some API variables to the templates and lets PHP and the template execute natively. It's nice, fast and efficient. (Other systems like EE actually execute PHP code in templates using eval(); which is slow and inefficient… they hope you won't be using much PHP). The way we'd have to implement a template engine in ProcessWire, while still retaining the speed, is with compiled templates. The template using template-tags would have to be compiled to a native PHP template before it could be executed. Lots of these new template engines are designed to work that way anyway, so not a big deal, but just outlining how it would be done.

#17 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,526 posts
  • 854

  • LocationVihti, Finland

Posted 11 June 2012 - 02:55 PM

Very well said Ryan, couldn't agree more. While I know I wouldn't use template language, I think that might well make PW a little bit more beginner friendly. Not sure if it is worth the trouble (vs. writing more tutorials, getting started materials etc..).

Also maybe we could cheat a little here, and tell that PW uses templating language called "Wire", which looks like this:

Output a variable:
<?= $page->title ?>

Loop for children:
<? foreach ($page->children as $p): ?>
<h2><?= $p->title ?>
<? endforeach; ?>

;)

#18 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3122

  • LocationAtlanta, GA

Posted 12 June 2012 - 04:18 PM

I like your idea of adding a "wire" template language! We can just stop referring to it as PHP and then all of the sudden people will see: wow this is a really easy template language. :)

#19 yellowled

yellowled

    Sr. Member

  • Members
  • PipPipPipPip
  • 183 posts
  • 117

  • LocationEutin, Germany

Posted 13 June 2012 - 01:01 AM

I think that might well make PW a little bit more beginner friendly.


I think it really depends on the kind of beginner you have in mind. Someone who knows HTML farely well and maybe has some idea of programming (let's say through JS), but no PHP knowledge might actually like something like i.e. Smarty. For someone with no programming experience, but willing to learn, it might not make a difference at all.

In my experience, what "scares" people about pure PHP templating is the syntax. It's pretty close to HTML, but introduces some things they usually don't have to deal with in pure HTML like single/double quotes, special characters etc. Most template engines use a syntax which is easier to distinguish from plain HTML at first glance. That's what "comforts" them.

Bottom line, as long as it's optional, a template engine might indeed attract more users without driving away the PHP-only folks. But beware – it might as well become Pandora's box. You add one template engine, people want another one … ;)

#20 Rob

Rob

    Sr. Member

  • Members
  • PipPipPipPip
  • 123 posts
  • 37

  • LocationLondon, UK

Posted 26 June 2012 - 11:52 AM

I've used twig templates, it's pretty easy once you have the twig bits set up to just call twig to spit out templates from the normal processwire PHP pages.

I have recently started a project where I use PW purely as a CMS tool, and then have a totally seperate site (sat side by side on the same server) that uses the PW API to pull data and inject into twig templates. I have used the Silex PHP microframework, but the API is so good you could use it from any PHP code you like. If you want to use PW as a CMS and page-modelling tool and then any other set of code to actually spit out a site to the end user, it's no problem.

I'm not a fan of HTML+PHP templates, and a template language like twig is much more powerful and flexible in many ways. Inheritance of templates, sandboxed template tags, ability to create custom tags, caching, very intelligent iteration and filtering/processing to do all kinds of stuff. Not a necessity by any means (and not an insult to PW), just shows that there are many options available that can utilise the real unique power of PW (the page/data modelling and the query API) and then whatever tool you feel comfortable with to build the site.

By using Silex, we have very fine control over our site structure. The site URL structure need not mimic how the pages (or generic pieces of data as we see it) are held in the PW page tree. We are basically using PW as a data/page repository (like a big XML document full of data), then using that data in whatever way we like with the API.

If you see PW as a data-modelling tool, rather than a page-modelling tool, it opens up a whole new way of thinking about things. Each "page" in PW is really just a unit of data, it need not actually represent a page if you don't want it to.

This was typed in a rush as I head out the door, so I hope it makes sense and gives some food for thought!





Also tagged with one or more of these keywords: template engine, twig, liquid markup, templates, mustache, handlebars, wp, php, simple, syntax

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users