Smes questions about a possible mustach use case

6 views
Skip to first unread message

Dj T@l

unread,
Nov 18, 2009, 5:35:50 AM11/18/09
to Mustache
Hello,

First congats for Mustache. I like the syntax and it seem to be simple
to use.
Then some questions :

For an app I'd like to use mustache to let user write their own page
and use some data their have on the application.
Like liquid but it seem to me than mustache is simpler than liquid

Do you think it's a good idea or mustach isn't designed for this kind
of task ?

--
Dj T@l

Chris Wanstrath

unread,
Nov 18, 2009, 12:35:01 PM11/18/09
to musta...@googlegroups.com
On Wed, Nov 18, 2009 at 2:35 AM, Dj T@l <guillaum...@gmail.com> wrote:

> For an app I'd like to use mustache to let user write their own page
> and use some data their have on the application.
> Like liquid but it seem to me than mustache is simpler than liquid
>
> Do you think it's a good idea or mustach isn't designed for this kind
> of task ?

Absolutely this is a good idea. I want to do the same thing on GitHub,
eventually.

--
Chris Wanstrath
http://github.com/defunkt

Dan Newman

unread,
Nov 18, 2009, 12:40:16 PM11/18/09
to musta...@googlegroups.com
are you guys talking about using mustache as a template lang for enabling users to modify their own profiles, pages, blog posts etc? similar maybe to what posterous, or tumblr does?

if so, add me to the list - mustache looks very ideal for this purpose.

i haven't started the process yet - but one question will be aside from mustache, just how one would best design this kind of system. ie., does the app create directories for each user with templates inside, and enable the user to edit/save there - or would the templates be stored in the db? how would the rails routing be setup for this? etc.
--
dev blog: www.polyrails.com
ph : 415-407-6239
::::::::::::::::::::::::::::::::::::::::::::::::::::::

Chris Wanstrath

unread,
Nov 18, 2009, 1:22:13 PM11/18/09
to musta...@googlegroups.com
On Wed, Nov 18, 2009 at 9:40 AM, Dan Newman <dpne...@gmail.com> wrote:

> are you guys talking about using mustache as a template lang for enabling
> users to modify their own profiles, pages, blog posts etc? similar maybe to
> what posterous, or tumblr does?

Yep!

> i haven't started the process yet - but one question will be aside from
> mustache, just how one would best design this kind of system. ie., does the
> app create directories for each user with templates inside, and enable the
> user to edit/save there - or would the templates be stored in the db? how
> would the rails routing be setup for this? etc.

I think you could use a DB for this pretty successfully.

Dan Newman

unread,
Nov 18, 2009, 1:46:32 PM11/18/09
to musta...@googlegroups.com
would the db approach scale do you think?

anyone want to take a stab at what the the controller and view look like if pulling the users mustache view code from the db?

Chris Wanstrath

unread,
Nov 18, 2009, 1:55:22 PM11/18/09
to musta...@googlegroups.com
On Wed, Nov 18, 2009 at 10:46 AM, Dan Newman <dpne...@gmail.com> wrote:

> would the db approach scale do you think?

I don't see why not. You could also gzip the data and store it in
memcached if you're concerned about memory footprint / speed.

> anyone want to take a stab at what the the controller and view look like if
> pulling the users mustache view code from the db?

You'd just need to setup a hash with all the data you'd like to make
available, then render that.

def show
@view = Views::Profile.new # Profile is a Mustache subclass
@view[:name] = @user.name
@view[:joined] = @user.created_at.to_s(:short)
end

in show.html.erb:

<%= @view.render %>

Dan Newman

unread,
Nov 18, 2009, 2:23:45 PM11/18/09
to musta...@googlegroups.com
great info ...thx much!

guillaume garcera

unread,
Nov 18, 2009, 2:53:47 PM11/18/09
to musta...@googlegroups.com
Ok

Nice to see that I've the same idea as other people


Le 18 nov. 2009 à 19:55, Chris Wanstrath a écrit :

On Wed, Nov 18, 2009 at 10:46 AM, Dan Newman <dpne...@gmail.com> wrote:

would the db approach scale do you think?

I don't see why not. You could also gzip the data and store it in
memcached if you're concerned about memory footprint / speed.

anyone want to take a stab at what the the controller and view look like if
pulling the users mustache view code from the db?

You'd just need to setup a hash with all the data you'd like to make
available, then render that.

def show
 @view = Views::Profile.new   # Profile is a Mustache subclass
 @view[:name] = @user.name
 @view[:joined] = @user.created_at.to_s(:short)
end

If I understand well you fetch the template from the db inside View::Profile class
just need to override the template search method.
Is it the good path to override 

  def self.template
    @template ||= templateify(File.read(template_file))
  end

in View::Profile class or simply affect the @template var in #initialize


in show.html.erb:

<%= @view.render %>


I like this no need to change rails internal neither to add a new render type in ActionView
And if we need to add some banner of footer we just add it to std rails view.

So I found my nightly  activity for the next days.
Yeah

--
guillaume garcera
twitter : https://twitter.com/djtal64



Dan Newman

unread,
Nov 18, 2009, 3:00:12 PM11/18/09
to musta...@googlegroups.com
if you can share what you come up on this list -- as you work through this ...that would be very cool to see!

yeah the ability to enable users to customize content through a flexible yet easy system, is very powerful.

Chris Wanstrath

unread,
Nov 18, 2009, 3:10:09 PM11/18/09
to musta...@googlegroups.com
On Wed, Nov 18, 2009 at 11:53 AM, guillaume garcera
<guillaum...@gmail.com> wrote:

>> def show
>>  @view = Views::Profile.new   # Profile is a Mustache subclass
>>  @view[:name] = @user.name
>>  @view[:joined] = @user.created_at.to_s(:short)
>> end
>
> If I understand well you fetch the template from the db inside View::Profile
> class

Right, I forgot about fetching the template from the db. Here is my
revised snippet:

def show
@view = Views::Profile.new # Profile is a Mustache subclass
@view.template = @user.profile_template # the string template
stored in the db
@view[:name] = @user.name
@view[:joined] = @user.created_at.to_s(:short)
end

You can use the `#template=` method on a Mustache instance to set the
template to an arbitrary string.

guillaume garcera

unread,
Nov 19, 2009, 2:02:39 PM11/19/09
to musta...@googlegroups.com
Hi,

After playing a little with Mustach and my app I've come with another
question.

Here my situation, the basis work, user can create a UserPage(stored
in the db) which contain the source for the template.
The rendering code work too (I use the technic you show before).

All is good.

Now I want to allow user to create partial and use them in their page.
If I understand well partial computing is implemented as follow in the
template class

# Partials are basically a way to render views from inside other
views.
def compile_partial(name)
klass = Mustache.view_class(name)
if klass != Mustache
ev("#{klass}.render")
else
src = File.read("#{@template_path}/#{name}.#
{@template_extension}")
compile(src)[1..-2]
end
end

I don't see how to search for the partial into the db.
My idea is to use the first part of the "if", so Mustache.view_class
(name) give me a custom class.
But how to tell this custom class to search in the db.

Is is better to simply use the other part of the if and replace
reading a file by searching into my db?

Chris Wanstrath

unread,
Nov 19, 2009, 4:33:43 PM11/19/09
to musta...@googlegroups.com
On Thu, Nov 19, 2009 at 11:02 AM, guillaume garcera
<guillaum...@gmail.com> wrote:

> Now I want to allow user to create partial and use them in their page.

Does the partial have a predefined name, or do the users create the name?

I would just do this yourself and ignore Mustache's partial syntax.

For example, let a user create a "widget" partial then add it for them
as a variable:

@user.partials.each do |partial|
@view["partial_#{partial.name}"] = partial.template
end

Then in their template:

<div id="widget">
{{partial_widget}}
</div>

guillaume garcera

unread,
Nov 19, 2009, 5:10:12 PM11/19/09
to musta...@googlegroups.com
Yes the name is given by the user

Your idea seem simpler than mine let's use it.

Thanks for your helps an the great lib.

Wayne Larsen

unread,
Nov 22, 2009, 2:11:04 PM11/22/09
to Mustache


On Nov 18, 12:55 pm, Chris Wanstrath <ch...@ozmm.org> wrote:
> You'd just need to setup a hash with all the data you'd like to make
> available, then render that.
>
> def show
>   @view = Views::Profile.new   # Profile is a Mustache subclass
>   @view[:name] = @user.name
>   @view[:joined] = @user.created_at.to_s(:short)
> end
>

So, you're talking about allowing the User to code the template, but
not the associated Mustache subclass? If so, the actual templates the
user would be able to create would be quite limited. To use your
example, the user would be limited to displaying the date formatted
asto_s(:short), since that's how you added it to the view, and
Mustache by design does not allow any logic or functions in the
templates.

I would imagine that for most people, liquid templates would be a
better option, since the templates allow a limited amount of logic,
and predefined filters (aka functions) give the template writer more
power, while preventing unwanted code execution and thus being usable
by untrusted users. Continuing your example, the user could write this
as {{ joined | date: "%a" }}

Now, if you're talking about allowing the user to write the view code
associated with the template, and still run in a trusted environment,
I'd be extremely interested. I think there is potential for view code
being javascript, and sandboxing the javascript code server side,
perhaps through Johnson. If anyone has ideas on this topic, I'd love
to hear them.


Dan Newman

unread,
Nov 22, 2009, 3:58:05 PM11/22/09
to musta...@googlegroups.com
Hmm.. i see your angle, but i wonder tho. Lqiuid templates with that level of logic and functionality may be too sophisticated for many to deal deal with. Question is who you're targetting I suppose. Can non programmers deal with liquid template functionality... the nice thing about mustache is the limitation and simplicity. I see posterous and tumblr being solid examples for targeting html/css builders. Enabling them a way to layout and customize. I don't believe there's much if any logic exposed. Seems like mustache is the closest thing to the kind of template system on those platforms.

guillaume garcera

unread,
Nov 24, 2009, 6:01:07 PM11/24/09
to musta...@googlegroups.com
Hi,

I've just tried this solution but it seem's that variable inside the partial are never interpolated .
 I have a partial like this :

<li>{{name}}</li>

included into a template like this

with partial now
{{#last_played}}
      <ul>
      {{#last_played_games}}
        {{partial_game}}
      {{/last_played_games}}
      </ul>
      {{/last_played}}

it output me

with partial now
    <li>{{name}}</li> <li>{{name}}</li> <li>{{name}}</li> <li>{{name}}</li> <li>{{name}}</li>
any idea ?

On other side I've used this tip to include user css (no variable inside) and it work very well.


Le 19 nov. 2009 à 22:33, Chris Wanstrath a écrit :

Chris Wanstrath

unread,
Nov 24, 2009, 7:43:38 PM11/24/09
to musta...@googlegroups.com
On Sun, Nov 22, 2009 at 11:11 AM, Wayne Larsen <wayne...@gmail.com> wrote:

> So, you're talking about allowing the User to code the template, but
> not the associated Mustache  subclass? If so, the actual templates the
> user would be able to create would be quite limited. To use your
> example, the user would be limited to displaying the date formatted
> asto_s(:short), since that's how you added it to the view, and
> Mustache by design does not allow any logic or functions in the
> templates.

Yep!

> I would imagine that for most people, liquid templates would be a
> better option, since the templates allow a limited amount of logic,
> and predefined filters (aka functions) give the template writer more
> power, while preventing unwanted code execution and thus being usable
> by untrusted users. Continuing your example, the user could write this
> as {{ joined | date: "%a" }}

Yes, Liquid templates are a better option if you want to give people
access to strftime formatting in the template itself.

> Now, if you're talking about allowing the user to write the view code
> associated with the template, and still run in a trusted environment,
> I'd be extremely interested. I think there is potential for view code
> being javascript, and sandboxing the javascript code server side,
> perhaps through Johnson. If anyone has ideas on this topic, I'd love
> to hear them.

Sure, but I think you're discounting Mustache for templating too
easily. It would still work great for letting users create their own
basic templates. Just depends on what your end goal is.

Chris Wanstrath

unread,
Nov 24, 2009, 7:46:13 PM11/24/09
to musta...@googlegroups.com
On Tue, Nov 24, 2009 at 3:01 PM, guillaume garcera
<guillaum...@gmail.com> wrote:

> I've just tried this solution but it seem's that variable inside the partial
> are never interpolated .
>  I have a partial like this :
> <li>{{name}}</li>
>
> included into a template like this
> with partial now
> {{#last_played}}
>       <ul>
>       {{#last_played_games}}
>         {{partial_game}}
>       {{/last_played_games}}
>       </ul>
>       {{/last_played}}
> it output me
> with partial now
>
> <li>{{name}}</li> <li>{{name}}</li> <li>{{name}}</li> <li>{{name}}</li>
> <li>{{name}}</li>

What does the partial_game code look like?

Wayne Larsen

unread,
Nov 25, 2009, 1:19:31 AM11/25/09
to Mustache
On Nov 24, 6:43 pm, Chris Wanstrath <ch...@ozmm.org> wrote:
> On Sun, Nov 22, 2009 at 11:11 AM, Wayne Larsen <waynelar...@gmail.com> wrote:
>
> Yes, Liquid templates are a better option if you want to give people
> access to strftime formatting in the template itself.

Well, that's just one example -- there's certainly other things you
can do in liquid that you can't do in mustache.

> > Now, if you're talking about allowing the user to write the view code
> > associated with the template, and still run in a trusted environment,
> > I'd be extremely interested. I think there is potential for view code
> > being javascript, and sandboxing the javascript code server side,
> > perhaps through Johnson. If anyone has ideas on this topic, I'd love
> > to hear them.
>
> Sure, but I think you're discounting Mustache for templating too
> easily.  It would still work great for letting users create their own
> basic templates. Just depends on what your end goal is.
>

Certainly -- depends on your end goal. If you can define in advance
all that the user needs, then mustache is a great solution. On the
other hand, I've pushed liquid templates to some pretty ugly places
(in mephisto) trying to make it do things it wasn't really designed to
do. Yet, at least it was possible. And that's why I was curious if you
were at all looking at allowing the user to add view code as well,
since I think it could be an extremely powerful solution -- providing
the best of both worlds.

guillaume garcera

unread,
Nov 25, 2009, 1:25:08 AM11/25/09
to musta...@googlegroups.com
Just as simple as : <li>{{name}}</li> it's for testing now but I plan
to have more variable and markup inside

Chris Wanstrath

unread,
Nov 25, 2009, 2:40:41 AM11/25/09
to musta...@googlegroups.com
On Tue, Nov 24, 2009 at 10:19 PM, Wayne Larsen <wayne...@gmail.com> wrote:

> Well, that's just one example -- there's certainly other things you
> can do in liquid that you can't do in mustache.

Like? Even that example is poor.

In a restricted environment I'd rather be given each individual piece
of time available and mix them on my own, e.g.

<span class="date">
{{day_of_the_month_sans_zeros}} {{full_month_name}} {{year_with_century}}
</span>

vs

<span class="date">
{{ joined | date: "%e %B, %Y" }}
</span>

This perfectly illustrates Mustache's strengths. The Mustache example
is basic but explicit while the Liquid example is complicated and
requires at least five different pieces of knowledge: available
filters, the concept of filters, how to pass arguments to filters,
available variables, and how to apply filters to variables.

Chris Wanstrath

unread,
Nov 25, 2009, 2:42:10 AM11/25/09
to musta...@googlegroups.com
On Tue, Nov 24, 2009 at 10:25 PM, guillaume garcera
<guillaum...@gmail.com> wrote:

> Just as simple as : <li>{{name}}</li> it's  for testing now but I plan to
> have more variable and markup inside

Mustache doesn't expand variables, you need to call Mustache.render for that.

def partial_game
Mustache.render(partial_game_from_db, :name => "Chris")
end

Without seeing more code it's hard to help here.

Chris

Chris Wanstrath

unread,
Nov 25, 2009, 2:51:35 AM11/25/09
to musta...@googlegroups.com
On Wed, Nov 18, 2009 at 9:40 AM, Dan Newman <dpne...@gmail.com> wrote:

> are you guys talking about using mustache as a template lang for enabling
> users to modify their own profiles, pages, blog posts etc? similar maybe to
> what posterous, or tumblr does?
>
> if so, add me to the list - mustache looks very ideal for this purpose.

Looks like start.io uses something suuuuuper similar to Mustache for
their templating:

http://start.io/help/layouts

Chris Wanstrath

unread,
Nov 25, 2009, 2:59:01 AM11/25/09
to musta...@googlegroups.com
On Tue, Nov 24, 2009 at 11:40 PM, Chris Wanstrath <ch...@ozmm.org> wrote:

> In a restricted environment I'd rather be given each individual piece
> of time available and mix them on my own, e.g.
>
> <span class="date">
>  {{day_of_the_month_sans_zeros}} {{full_month_name}} {{year_with_century}}
> </span>

Looks like Tumblr agrees: http://www.tumblr.com/docs/custom_themes#dates

Wayne Larsen

unread,
Nov 25, 2009, 1:31:52 PM11/25/09
to Mustache


On Nov 25, 1:40 am, Chris Wanstrath <ch...@ozmm.org> wrote:
> On Tue, Nov 24, 2009 at 10:19 PM, Wayne Larsen <waynelar...@gmail.com> wrote:
> > Well, that's just one example -- there's certainly other things you
> > can do in liquid that you can't do in mustache.
>
> Like? Even that example is poor.

In tumblr, if you want to insert some markup between the next and
previous links only if both are on the page (ie "Previous | Next")?
PreviousPage and NextPage exist, but not PreviousPage_and_NextPage

> In a restricted environment I'd rather be given each individual piece
> of time available and mix them on my own, e.g.
>
> <span class="date">
>   {{day_of_the_month_sans_zeros}} {{full_month_name}} {{year_with_century}}
> </span>
>
> vs
>
> <span class="date">
>   {{ joined | date: "%e %B, %Y" }}
> </span>
>
> This perfectly illustrates Mustache's strengths. The Mustache example
> is basic but explicit while the Liquid example is complicated and
> requires at least five different pieces of knowledge: available
> filters, the concept of filters, how to pass arguments to filters,
> available variables, and how to apply filters to variables.
>

I'm not trying to defend liquid in any way. Nor am I trying to
criticize mustache. Like you've pointed out, Mustache can do a lot
more than you'd think at first glance without full support for
conditional logic in the templates. It does require the variables and
use cases to be thought of ahead of time. For cases such as tumblr or
start.io (which does have conditional logic btw), it's probably a
great solution.

I was simply hoping the above example could be written like so:

<span class="date">{{ my_joined_date }}</span>

def my_joined_date
date.strftime("%e %B, %Y")
end

Different use cases, different target users.


guillaume garcera

unread,
Nov 25, 2009, 1:51:11 PM11/25/09
to musta...@googlegroups.com
Hi,

If this can help here the code I use : http://gist.github.com/242925
With your technique I must create one method per partial. Is is not
possible to delegate partial finding to the view class(ie the instance
of Mustache/Mustache subclass.
If not possible I can try to automatically generate theses method just
before rendering the main template.

Chris Wanstrath

unread,
Nov 27, 2009, 12:25:57 AM11/27/09
to musta...@googlegroups.com
On Wed, Nov 25, 2009 at 10:51 AM, guillaume garcera
<guillaum...@gmail.com> wrote:

> If this can help here the code I use : http://gist.github.com/242925
> With your technique I must create one method per partial. Is is not possible
> to delegate partial finding to the view class(ie the instance of
> Mustache/Mustache subclass.
> If not possible I can try to automatically generate theses method just
> before rendering the main template.

You need to render the partial yourself in this case. Luckily it's not
hard. I've modified http://gist.github.com/243836 with the needed
changes.

Basically this:

account.user_pages.partials.each do |partial|
@view["partial#{partial.to_partial_name}"] =
Mustache.render(partial.template, @view.context)
end

Chris
Reply all
Reply to author
Forward
0 new messages