bminer / node-blade

Blade - HTML Template Compiler, inspired by Jade & Haml
Other
320 stars 28 forks source link

using locals.settings vars in blade.Runtime.loadTemplate #133

Closed nkhine closed 11 years ago

nkhine commented 11 years ago

not sure why i get this error:

Uncaught TypeError: Cannot read property 'translation' of undefined when i try to pull the data using the

 - var guide = locals.settings.translation.guide

as the routes have been set in my express application, i can call this same in my footer.blade template with no issues, but not in this case, when the template is loaded via the blade.Runtime.loadTemplate

i basically would like to replace the tmpl { ...} array with the output of locals.settings.translation.guide is this possible or is there a better way in doing this?

bminer commented 11 years ago

You have to pass the settings Object to that view.

nkhine commented 11 years ago

sorry, but how would i do this, as i thought this was already done in server.js?

bminer commented 11 years ago

I don't think that I understand your question. Could you please clarify?

nkhine commented 11 years ago

i have the following server.js code

app.use(app.router);
app.set('view engine', 'blade'); //Yes! Blade works with Express out of the box!
//app.set('views', __dirname);
app.set('views', __dirname + '/views'); //tells Express where our views are stored
try {
    app.set('languages', require(__dirname + '/locales/config.json'));
    app.set('translation', require(__dirname + '/locales/dev/translation.json'));
    app.set('chapters', require(__dirname + '/data/chapters.json'));
} catch(err) {
  dumpError(err);
  console.log('there is no /data/chapters.json');
  app.set('chapters', []);
}

so from within any .blade templates i am able to for example have:

    - var guides = locals.settings.translation.guide
    - console.log(guides)

and in the console this would return, something like:

    { sections: 
       { 'the-basics': { title: 'The Basics', contents: [Object] },
         'setting-up-a-national-chapter': { title: 'Setting up a National Chapter', contents: [Object] } } }

but if i have a view which is loaded via the blade.Runtime.loadTemplate

     - var guides = locals.settings.translation.guide

i get an error:

    Uncaught TypeError: Cannot read property 'translation' of undefined

here is the guides_section.blade output, the code that generates it is https://github.com/TZM/tzm-blade/blob/master/public/js/footer.js#L74

blade._cachedViews["guide_section.blade"]=function anonymous(locals, cb, __) {
    __ = __ || [];
    __.r = __.r || blade.Runtime;
    if (!__.func) __.func = {}, __.blocks = {}, __.chunk = {};
    __.locals = locals || {};
    __.filename = "guide_section.blade";
    try {
        with (__.locals) {
            __.line = 1, __.col = 1;
            var guide = locals.settings.translation.guide;
            __.line = 2, __.col = 1;
            __.push("<div" + ' id="guide_section"' + ">");
            __.line = 3, __.col = 5;
            var i = sections.section;
            __.line = 4, __.col = 5;
            var c = sections.subsection;
            __.line = 5, __.col = 5;
            __.push("<h5");
            __.r.attrs({
                "data-i18n": {
                    v: "guide.sections." + i + ".title",
                    e: 1
                }
            }, __);
            __.push(">" + __.r.escape(i18n.t("guide.sections." + i + ".title")) + "</h5>");
            __.line = 6, __.col = 5;
            __.push("<h4");
            __.r.attrs({
                "data-i18n": {
                    v: "guide.sections." + i + ".contents.title",
                    e: 1
                }
            }, __);
            __.push(">" + __.r.escape(i18n.t(c)) + "</h4>" + "</div>");
        }
    } catch (e) {
        return cb(__.r.rethrow(e, __));
    }
    if (!__.inc) __.r.done(__);
    cb(null, __.join(""), __);
};if(blade._cb["guide_section.blade"])blade._cb["guide_section.blade"](".",[],false);

so i get Uncaught TypeError: Cannot read property 'translation' of undefined

whereas on a template that is not loaded using the blade.Runtime.loadTemplate

blade._cachedViews["index.blade"]=function anonymous(locals, cb, __) {
    __ = __ || [];
    __.r = __.r || blade.Runtime;
    if (!__.func) __.func = {}, __.blocks = {}, __.chunk = {};
    __.locals = locals || {};
    __.rel = ".";
    __.filename = "index.blade";
    try {
        with (__.locals) {
            __.line = 1, __.col = 1;
            __.r.include("../layout/layout.blade", __);
            __.line = 2, __.col = 1;
            __.r.blockRender("a", "title", __, "Homepage");
            __.line = 3, __.col = 1;
            var guide = locals.settings.translation.guide;
            __.line = 4, __.col = 1;
            console.log(guide);
        }
    } catch (e) {
        return cb(__.r.rethrow(e, __));
    }
    if (!__.inc) __.r.done(__);
    cb(null, __.join(""), __);
};if(blade._cb["index.blade"])blade._cb["index.blade"](".",["../layout/layout.blade"],false);

works fine!

what am i missing here, i would like to replace the https://github.com/TZM/tzm-blade/blob/master/public/js/footer.js#L74 guide array with the one available in locals.settings.translation.guide

as a fix, i thought i would use the jquery ajax to this but it does not seem right to me!

any advise much appreciated

bminer commented 11 years ago

Ah! View helpers on the server are not available to the Blade runtime in the browser. That might be a feature someone might want to add to the Blade middleware in the future. Sometimes exposing these view helpers might not make sense, or it might even pose a security risk.