jsmart: no se puede obtener el contenido de la plantilla (¿a tiempo?)

Estoy tratando de usar jsmart para hacer Sabelotodo3 templates on the client-side. If you've no experience with them, keep reading because it could just be a simple JavaScript error that I'm making.

It works for simple templates:

I create the template (which I receive via AJAX), then render it (passing it the data), as per the documentation:

var template = new jSmart(templateReceivedViaAJAX);
var content = template.fetch({"firstname":"adam", "secondname":"lynch"});

Then I simply stick the rendered output in a div:


Herencia de plantilla

Problems happen when trying to render templates which contain include, extends, etc.

Desde el documentación:

Whenever jSmart come across either of template inclusion tags it calls jSmart.prototype.getTemplate() method and passes it a value of tag's file parameter. The method must return the template's text.

The default implementation of getTemplate() throws an exception. So, it is up to a jSmart user to override this method and provide template's text.

Overridding the getTemplate() función:

jSmart.prototype.getTemplate = function(name) {
    $.ajax({type: 'GET', url: name, async:false, success: function(data) {
        console.log('got template at '+name+'. The following is the contents:');
        return data;

The console output when rendering a con el futuro bebé template containing an include llamar a un sus hijos modelo:

<div class="row">

    <label for="second" class="span4">Second Name:</label>  

    <input type="text" class="span4" placeholder="{$secondname}" id="second" /> 


Uncaught Error: No template for /bundles/templatedemo/templates/form_include.html.smarty 

The console output when rendering a sus hijos template containing an extend llamar a un con el futuro bebé modelo:

got template at /bundles/templatedemo/templates/form.html.smarty. The following is the contents: templates:58
<form class="well">  

    <div class="row">

        <label for="first" class="span4">First Name:</label>  

        <input type="text" class="span4" placeholder="{$firstname}" id="first" /> 


    {block name=form_include}{/block}

    <input type="submit" class="btn btn-inverse" />  

Uncaught Error: No template for /bundles/templatedemo/templates/form.html.smarty 
(anonymous function) 
(anonymous function) 


Inheritance works if the template contents are there in advance (if they're hard-coded in, instead of being retrieved via AJAX, for example).

preguntado el 31 de julio de 12 a las 13:07

I'll try to test jSmart against SmartyBundle later, tonight (GMT) to see if this is SmartyBundle related. I'm also available on the #noiselabs freenode channel if you want to debug it live. -

@noisebleed Thanks! I'll be there all day (GMT+1) -

1 Respuestas

Use a function pointer rather than an anonymous function:

function foo() 
  $.ajax({type: 'GET', url: foo.url, async:false, success: bar});

function bar(data) 
  console.log(['got template at', foo.url, 'The following is the contents:']);

  return data;

foo.url = "http://www.stackoverflow.com";

jSmart.prototype.getTemplate = foo;

respondido 05 nov., 13:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.