Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:00:54+00:00 2026-06-17T09:00:54+00:00

CONTEXT: I have read as much Mustache documentation as possible but I cannot get

  • 0

CONTEXT:

I have read as much Mustache documentation as possible but I cannot get my head around how to use partials or even whether I am using Mustache in the correct way.

The code below is working correctly. My problem is that I have three Mustache files that I want to include and render all at once.

I am guessing that this is what partials are meant for but I cannot seem to make it work.


QUESTIONS:

How I would get partials working in this context so that my three Mustache files are being loaded and are all being passed the $data variable?

Should I be using file_get_contents in this manner for the template? I have seen Mustache functions being used in its place but I cannot find extensive enough documentation to get it working.


ENV:

I am using the latest version of Mustache from https://github.com/bobthecow/mustache.php

My files are:
index.php ( below )
template.mustache
template1.mustache
template2.mustache
class.php


CODE:

// This is index.php
// Require mustache for our templates
require 'mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();

// Init template engine
$m = new Mustache_Engine;

// Set up our templates
$template   = file_get_contents("template.mustache");

// Include the class which contains all the data and initialise it
include('class.php');
$data = new class();

    // Render the template
print $m->render( $template, $data );

THANK YOU:

Any examples of a PHP implementation of partials ( including how the necessary file structure would need to be ) would be greatly appreciated, just so I am able to get a solid understanding 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:00:54+00:00Added an answer on June 17, 2026 at 9:00 am

    The simplest is to use the “filesystem” template loader:

    <?php
    // This is index.php
    // Require mustache for our templates
    require 'mustache/src/Mustache/Autoloader.php';
    Mustache_Autoloader::register();
    
    // Init template engine
    $m = new Mustache_Engine(array(
        'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__))
    ));
    
    // Include the class which contains all the data and initialise it
    include('class.php');
    $data = new class();
    
    // Render the template
    print $m->render('template', $data);
    

    Then, assuming your template.mustache looks something like this:

    {{> template2 }}
    {{> template3 }}
    

    The template2.mustache and template3.mustache templates would be automatically loaded from the current directory when needed.

    Note that this loader is used for both the original template and the partials. If you have your partials stored in a subdirectory, for example, you can add a second loader specifically for partials:

    <?php
    $m = new Mustache_Engine(array(
        'loader'          => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'),
        'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials')
    ));
    

    There’s more info on these and other Mustache_Engine options on the Mustache.php wiki.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read around it appears this should be possible. I have the following content
I have created very simple app with persistence context (hibernate as provider) to read
I am starting to use Django and while I have read the book and
I have read through the facebook developers documentation and it is not clear to
I have read so much (dozens of posts) about one thing: How to unit
I have been trying to figure out how to read paragraph content which exists
I have this command in a Rails controller open(source) { |s| content = s.read
I have an XML file as follows, and I'm trying to read the content
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
Context I have a live running redis-server. I want to make a backup. Idea:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.