English 中文(简体)
Mustache partials and code reuse
原标题:

I m getting the hang of mustache for a project I ve started during the weekend.

I m using the PHP implementation. I have, however a couple of inquiries as I m not used to the system.

How do you handle template inheritance, or reuse? I know of partials, but how should I use them? I m doing something like this, ala include:

top.mustache:

<!DOCTYPE html>
<html lang= es >
<head>
    <meta charset=utf-8" />
    <link rel="stylesheet" href="/media/style.css" type="text/css" media="screen" />
</head>
<body>
     <header><h1><a href="/">Top</a></h1>
     </header>
     <section>

bottom.mustache:

        </section>
        <footer><a href="http://potajecreativo.com/">potaje</a></footer>
</body>
</html>

And a view to render this template:

{{>top}}
<form action="/album/" method="post">
    <p><label for="name">Name</label> <input type="text" name="name" value=""/></p>
    <p><label for="description">Description</label> <textarea name="description" rows="8" cols="40"></textarea></p>
    <p><input type="submit" value="Save" /></p>
</form>
{{>bottom }}

Is this the right approach?

最佳回答

ynkr s answer is right for older versions, but I just upgraded to version 2.4.1 and there your approach should work if you re using the filesystemloader.

See https://github.com/bobthecow/mustache.php/wiki/Template-Loading#partials-loading for details.

问题回答

Here is an example of how the php implementation of Mustache works. Of note is that Mustache.php will not expand the included partials/templates, so you have to hand them to mustache as seen below. This example was pieced together on an older cakephp framework.

<?
  # php cannot recognize multiple acceptable file extensions for partials,
  # so toggle it to mustache s extension
  $this->ext =  .mustache ;

  # Mustache says it s logic-less, but that s not exactly true.
  # Render out the basic header which calls the logo partial and has
  # a {{# logged_in? }} condition which dictates which user box we
  # show. Thus, we need to render out the html for both the logged in
  # and logged out user boxes
  $basic_header_html = $this->renderElement( basic_header );
  $logo       = $this->renderElement( shared/logo );
  $logged_in  = $this->renderElement( shared/logged_in_user_box );
  $logged_out = $this->renderElement( shared/logged_out_user_box );

  $m = new Mustache($basic_header_html,                    
                    array( logged_in?  => !empty($this->Auth->userData),
                           cache_buster  => time(),
                           display_name  =>  StackOverflow Customer ),
                    array( shared/logo  => $logo,
                           shared/logged_in_user_box  => $logged_in,
                           shared/logged_out_user_box  => $logged_out));
?>

<!DOCTYPE html>
<html>
<head>
  <title>Mustache Test</title>
</head>

<body>
  <?= $m->render(); ?>
</body>
</html>

basic_header.mustache

<div id="header" class="basic">
  {{> shared/logo }}

  {{# logged_in? }}
    {{> shared/logged_in_user_box }}
  {{/ logged_in? }}

  {{^ logged_in? }}
    {{> shared/logged_out_user_box }}
  {{/ logged_in? }}
</div>

shared/logo.mustache

<a class="logo" href="/foo/bar"><img alt="" src="/images/logo.png?{{ cache_buster }}" /></a>

shared/logged_in_user_box.mustache

Hello {{display_name}}, you are logged in.

shared/logged_out_user_box.mustache

Hello. You are not logged in.




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签