Jump to content

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.com/api/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.

  • Like 2

Share this post


Link to post
Share on other sites

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

  • Like 2

Share this post


Link to post
Share on other sites

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 ;)

  • Like 5

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

  • Like 2

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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 :)

  • Like 4

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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
  • Like 2

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 ;)

  • Like 1

Share this post


Link to post
Share on other sites

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.

  • Like 6

Share this post


Link to post
Share on other sites

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; ?>

;)

  • Like 6

Share this post


Link to post
Share on other sites

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. :)

  • Like 3

Share this post


Link to post
Share on other sites

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 … ;)

  • Like 4

Share this post


Link to post
Share on other sites

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!

  • Like 1

Share this post


Link to post
Share on other sites

Rob, very interesting. I found one of the best parts of PW is that it has strong relation to websites, ie. each page has an unique url (wheter you need it or not), nice caching, good ua control etc. So I find almost everything pw gives granted very useful and like to keep those features. But I also love the bootstrapping and using API just as a data storage, it is very flexible. I found your approach pretty radical (building whole site as bootstrapping), but very interesting and want to try it too (please do share more information, if possible). PW definitely doesn't get into your way in that method, but you get great API and admin site for free. Nice!

PS: Have to say that Twig actually looks pretty interesting.

Share this post


Link to post
Share on other sites

Apeisa - I actually agree with most of those points and even using PW as-is, without any extra flourish, it's still much easier and more flexible than 90% of other CMS I have used in 10+ years of web development, and would do fine "out of the box" for 90%+ of typical web agency situations.

Regarding the use of Silex with PW, it's really very simple. Silex is a microframework that provides a very simple mechanism for mapping regex-based URLs to a methods (actually closures, so PHP 5.3+ only), where you can stick some code. Because it is based on Symfony2, it is built to a very robust standard with best practise in mind and can make easy use of many great Symfony2 components such as Request/Response objects (HTTP handling), Twig templates, DB access with Doctrine, Session handling and many other useful bits.

In a nutshell - you map a URL to a method and in that method you use the PW API to grab data and inject into a Twig template. That's it. Your URL structures can be as simple or complex as you like with placeholders for dynamic parts e.g. canonical URL segment of blog post or the DB record ID etc (just the variable bits) and you can enforce regex rules on those parts to ensure they match a certan format.

It might seem like a lot of fuss, but it isn't really and the Twig templates are a joy. They're clean, secure, you can extend with new tags, perform all sorts of processing and iterating of data, and templates can extend each other so you can have more generic templates that get extended with specific functionality as you need it. It measn that building a slight modification of an existing template can be done very easily and in a way that doesn't have to tamper with the original at all, just like a class hierarchy in OO code.

http://silex.sensiolabs.org/

http://twig.sensiolabs.org/

I'm a bit of a Sensiolabs fanboy to be honest, and I think Fabien Potencier and the Sensiolabs team come across as being the cutting edge of PHP development. Symfony2 is a great framework, Twig is brilliant, Silex is very easy but powerful and everything they do seems to have a real sense of quality craftmanship. In combination with the excellent data modelling power (and excellent back-end interface) of Processwire, it's a good combo!

  • Like 2

Share this post


Link to post
Share on other sites

Just wanted to add to this discussion that one of the clear benefits of using templating engines is being able to share templates and partials between different programming languages, and thus between client side and server side aspects of your site. This is very handy when building web apps or heavily ajax-ified sites.

Share this post


Link to post
Share on other sites

One day we will port the "Wire" template language to javascript :)

Share this post


Link to post
Share on other sites

Didn't read all of your posts so maybe sorry if anyone else said this before but:

It isn't a huge difference if you have to learn a special wordpress template language which is much smaller then PHP or a little bit of PHP.

Like for example:

Wordpress-way:

<?php the_title(); ?>

PW-way:

<?php echo $page->title; ?>

It isn't more complicated or much longer. And also I'm loving it hove processwire handles this ;)

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 ngrmm
      I have a page which sends out an email based on queries in the url
      <?php // event ID $eventID = $input->get('eventID','int'); $event = $pages->get($eventID); // config $adminEmail = "events@test.com"; $fromEmail = "noreply@test.com"; $fromName = "test"; $emailSubject = "Test Email"; // HTML BODY $emailBody = ""; // HOW TO ??? … // send mail $m = new WireMail(); $m->to($adminEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); ?> The email body is a bit complex: standard html/css tables and some php (pw variables).
      I put my emailbody (html) in a seperate file emailbody.inc but don't know how to include it?
       
    • By cboetens
      Hi guys
      I guess this is the right forum to post my question. If not, do let me know. Ok, I'm fairly new to ProcessWire and so far I really like the CMS! It's magnificent to use and really straight forward. Big thanks to Ryan for that.
      Now, the question I have. I have a template called 'products_categories.php' in which I collect and render all the different categories (the categories basically act as a filter) for a variety of products and next to the categories, I render all the products, like so:
      <?php include('includes/header.php') ?> <?php $categoryName = $sanitizer->pageName($input->urlSegment(1)); $subcategoryName = $sanitizer->pageName($input->urlSegment(2)); pre($categoryName); pre($subcategoryName); $categories = $page->get("template=products_categories"); $category = $categories->get("template=products_categories_item, name=" . $categoryName); if($category->id){ $session->category = $category->id; $subcategory = $category->get("template=products_categories_item, name=" . $subcategoryName); if($subcategory->id){ $session->subcategory = $subcategory->id; } } $selector = "template=products_item"; if($category->id){ $selector .= ", products_categories=" . $category; } if(isset($subcategory->id)){ $selector .= ", products_categories=" . $subcategory; } $products_items = $page->find($selector); ?> <div class="uk-section"> <div class="uk-container"> <?php if(!empty($page->headline)) { ?> <h1><?= $page->headline ?></h1> <?php } ?> <!-- **** COMMENT: create grid for products **** ---> <div class="row"> <div class="col-md-3"> <div class="uk-card mr-3 uk-card-default uk-card-body"> <h3 class="uk-card-title">Productcategorieën</h3> <ul class="uk-list"> <?php foreach($categories->children() as $c){ ?> <li class="<?= ($category->id===$c->id ? 'active ' : '') ?>"> <a href="<?= $page->url . $c->name . '/' ?>"><?= $c->title; ?></a> <?php if($c->hasChildren){ ?> <ul> <?php foreach($c->children() as $sc) {?> <li class="<?= ($category->id==$c->id && $subcategory->id==$sc->id ? 'active ' : '') ?>"> <a href="<?= $page->url . $c->name . '/' . $sc->name . '/' ?>"><?= $sc->title; ?></a> </li> <?php } ?> </ul> <?php } ?> </li> <?php } ?> </ul> </div> </div> <div class="col-md-9"> <div class="uk-child-width-1-3@s uk-grid-match uk-grid-margin-small uk-grid-small" uk-grid> <?php foreach($products_items as $product) { ?> <div class="uk-card"> <?php if(isset($product->image)) { ?> <div class="uk-card-media-top"> <img src="<?= $product->image->URL; ?>" title="<?= $product->title; ?>" alt="<?= $product->intro; ?>"> </div> <?php } ?> <?php if(!empty($product->title) || !empty($product->intro)) { ?> <div class="uk-card-body uk-card-default"> <?php if(!empty($product->title)) { ?> <h3 class="uk-card-title"><?= $product->title; ?></h3> <?php } ?> <?php if(!empty($product->intro)) { ?> <p><?= $product->intro; ?></p> <?php } ?> <?php if (!empty($product->price)): ?> <h3 class="uk-card-title">&euro;&nbsp;<?= $product->price; ?>&nbsp;(excl. btw)</h3> <?php endif; ?> <a class="uk-button uk-button-primary" href="<?= $product->url; ?>">Bekijk</a> </div> <?php } ?> </div> <?php } ?> </div> </div> </div> </div> </div> <?php include('includes/footer.php'); ?> Now, I can't seem to get the filter to work. The URL behind every (sub)category should go straight back to the template 'products_categories.php', that way I can get a range of products according to the selected/ clicked URL.
      What am I missing here? Is this not the correct way to handle things?
      Any help is welcome!
      Thanks.
      Cédric
    • By Mithlesh
      Hi there,
      My form is not getting submitted, it is showing:
      Unable to verify successful email delivery of this form submission.
      Attaching for your reference as well: 

      In the Backend, it is showing Connection timed out with smtp.gmail.com
      Pl guide me how to resolve that
    • By CareerTeam GmbH
      Hi there,
      We are an executive search agency based in Germany looking for a freelancer (2-5 days per week) supporting us with the development and design of our websites. The position will be located in Hamburg, Germany and it would be great if you are on short call. German language knowledge is mandatory. 
      You can reach me via email jobs@careerteam.de.
      Thank you!
      Regards
      Annemie
    • By louisstephens
      I was really unsure of how to actually title this post, so I do apologize (if someone has a better idea, I will gladly edit it). I am using the profields: pagetable field to allow people to create their own "content" (copy, image, button, etc etc) and rearrange it. I also included a field called "column_size" using the RangeSlider set to (1-12).
      I guess I'll clarify a bit more on this. I am using flexbox where the "row" is <section></section> and the columns are <div class="column"></div> have given the "columns"  flex: 1 1 0; so no matter how many columns you have, the columns will automatically adjust for new content. Where my confusion is coming in: If a user has set up 3 copy items (with 12, 5, 7 respectfully for the column_size), how do I actually output this in my template? I was going to use a switch statement to handle the various items which I thought made it quite easy, but with closing sections and columns I have confused myself as I assume I need an if statement to check if the column size is > 12, or = 12 to determine the actual closing/opening of sections. I apologize if I have not made this very clear. I am a bit unsure how to word this let alone to go about this. 
      Im very appreciative of for any insight into this.
       
       
×
×
  • Create New...