bobthecow / mustache.php

A Mustache implementation in PHP.
http://mustache.github.io/
MIT License
3.25k stars 420 forks source link

Customizing delimiters #194

Closed jslegers closed 10 years ago

jslegers commented 10 years ago

While I really like the features of Mustache, I don't like its delimiters. For example, Netbeans struggles with auto-formatting Mustache files. Also, I don't think it's nearly as readable as other options.

I would like the possibility to customize the default delimiters as shown below.


Current :

<html>
    <head>
        <#styles}}
          {{# links }}{{ url }}{{/ links }}
        {{/styles}}
    <title>{{title}}</title>
    </head>
    <body>
        {{{content}}}
    </body>
</html>

Alternative 1 :

<html>
    <head>
        <_.styles._>
            <_.links._>
                <_.url._ />
            </_.links._>
        </_.styles._>
        <title>
            <_.title._ />
        </title>
    </head>
    <body>
        <_.content._ />
    </body>
</html>

Alternative 2 :

<html>
    <head>
        <TEMPLATE-styles>
            <TEMPLATE-links>
                <TEMPLATE-url />
            </TEMPLATE-links>
        </TEMPLATE-styles>
        <title>
            <TEMPLATE-title />
        </title>
    </head>
    <body>
        <TEMPLATE-content />
    </body>
</html>

Alternative 3 :

<html>
    <head>
        <?ts styles ?>
            <?ts links ?>
                <?t url ?>
            <?te links ?>
        <?te styles ?>
        <title>
            <?t title ?>
        </title>
    </head>
    <body>
        <?t content ?>
    </body>
</html>

Alternative 4 :

<html>
    <head>
        <!--s styles -->
            <!--s links -->
                <!-- url -->
            <!--e links -->
        <!--e styles -->
        <title>
            <!-- title -->
        </title>
    </head>
    <body>
        <!-- content -->
    </body>
</html>

Alternative 5 :


<html>
    <head>
        <%s styles %>
            <%s links %>
                <% url %>
            <%e links %>
        <%e styles %>
        <title>
            <% title %>
        </title>
    </head>
    <body>
        <% content %>
    </body>
</html>

Alternative 6 :

<html>
    <head>
        << styles >>
            << links >>
                << url />>
            <</ links >>
        <</ styles >>
        <title>
            << title />>
        </title>
    </head>
    <body>
        << content />>
    </body>
</html>

Note that alternatives 1 to 4 are valid XML and alternative 5 is even valid HTML.

jslegers commented 10 years ago

See also https://github.com/janl/mustache.js/issues/360

davethegr8 commented 10 years ago

I think that would be a cool thing also, but I don't think it's part of the mustache spec. Note, you can change delimiters within a template file, outlined in the "Set Delimiter" section of http://mustache.github.io/mustache.5.html

jslegers commented 10 years ago

@davethegr8 : I know you can change delimiters within a template file, but that doesn't really help.

If I want the same non-standard delimiters for all of my templates, that means I would have to add stuff like {{=<% %>=}} on top of every single template. That's about as lacking in DRY-ness as it gets and doesn't allow me to make my templates fully XML or HTML compliant.

With regards to the mustache spec, is there a seperate Github repo for that I can post this issue for? I did post it at https://github.com/janl/mustache.js/issues/360, as the main reason I'm interested in Mustache is being able to use the same templates both backend and frontend.

mattdeclaire commented 10 years ago

You could create your own template loader that prepends the delimiter switch to each template it renders.

jslegers commented 10 years ago

@mattdeclaire :

That's a very messy and "hacky" workaround, which I'd like to avoid at all costs.

I'd prefer to use a different templating engine or write one of my own...

mattdeclaire commented 10 years ago

Don't throw out the baby with the bath water.

bobthecow commented 10 years ago

XML / HTML compliant template languages are really terrible in practice. Have you ever used Spring templates? It's all well and good, until you need to put a value in an attribute:

<a href="<url/>">
  <text/>
</a>
jslegers commented 10 years ago

@bobthecow :

I'm currently working on a widget engine / framework in PHP. For this project, it is essential to have a templating system with readable templates that provides a user-friendly way to include reusable components that can have any number of variables,

I have looked into TAL ( http://phptal.org/ ) as an alternative to Mustache, but I don't like the way they mix their TAL-attributes with their regular HTML. It seems a rather unreadable mess to me.

What I like about languages like Mustache and Handlebars is their simplicity for adding simple variables, but IMO they also tend to get increasingly unreadable and messy when you want something more complex. More in particular, I'm thinking of nested callables, which IMO seems rather essential for the widgeting system I'm working on.

bobthecow commented 10 years ago

@jslegers It really sounds like you don't want to use Mustache, and that's okay :)

It's not possible, for example, to use different closing delimiters for "self closing" tags and section tags. It's also not possible to change the tokens that indicate what a section is. If you're using Mustache, you're stuck with using #, >, ^, and so forth in your tags. It's conceivable that a Mustache implementation could make all of those configurable, but it would come at a pretty hefty performance cost, since now each loop of tokenizing and parsing would have to look up the current configured tokens rather than using constants. It's not really worth slowing things down for all users, just for one exceptional case.

jslegers commented 10 years ago

@bobthecow :

I'm looking for a templating language that's both powerful and readable enough for a widgeting system that uses the same templates in frontend and backend. Mustache suits my needs in some areas... but not in others... yet it's still one of the best options I've found so far.

I have the same problem with Sass ( http://sass-lang.com/ ). Sass is the only remotely decent pre-processor language for CSS, yet it's still terribly flawed in many ways. It's frustrating, really, to be stuck with templating and pre-processor languages that only partially do what you want and to fail to convince the dev team that your feature requests would be important improvements to the language. This is ironic, really, since there are still loads of people who reject even the very concept of templating and pre-processor languages precisely because of such problems...

I could - of course - always create my own templating and pre-compiler languages, but I've already been spending way too much time on framework development and can't really afford spending much time on projects that are even lower level building blocks.

To summarize :

I would like to use both Mustache and Sass for my projects, but both languages are too limited for my use cases and I'm incredibly frustrated by both the disinterest of the core devs and the lack I time (and/or budget) I have for writing an alternative myself.

bobthecow commented 10 years ago

Even if we did make delimiters and section/tag tokens customizable, you wouldn't have what you want. You really need an XML parser based templating language to do what you're looking for, and changing Mustache delimiters and tokens wouldn't turn it into that. Hence my comment that you're looking for something that's not Mustache.

Please consider that good projects are often good in part because they do not accept all feature requests. In this case, your preferences for "flexible and customizable" are at odds with the Mustache project goals of "simple, compatible and fast". To implement your feature request would mean a compromise. In your opinion, that compromise is worth it, because it would let you change Mustache into the language you wish it was. In my opinion, it's not a worthwhile compromise because it prioritizes the preference of one user over the needs of every other user.

jslegers commented 10 years ago

@bobthecow :

IMO the most optimal templating strategy would involve simple regex for basic variable replacement and a more advanced technique (eg. XML parsing within well-defined boundaries) for more advanced templating. Mustache does really well in the first area, but fails to deliver in the second area.

As someone who created his own Open Source framework ( http://www.cascade-framework.com/ ), I understand your position. I understand perfectly why you can't throw in every wild idea. However, I also see how a reluctance to think out of the box restricts the potential of open source in many ways.

Open Source is a great concept in theory, but the reality is that it leads to a very fragmented landscape of mostly mediocre projects. No one wants to take the time or effort to analyse other people's ideas and incorporate into their project, so everyone keeps reinventing the wheel. IMO this indicates that something has gone horribly wrong!

We should centralize our efforts and collaborate to build stuff that's both very flexible and great in performance instead of all going our own way and deliver something that's mediocre in both areas.