Mostrando las entradas con la etiqueta mustache. Mostrar todas las entradas
Mostrando las entradas con la etiqueta mustache. Mostrar todas las entradas

viernes, octubre 22, 2010

CouchApp IV: creando funciones show con templates HTML

necesitamos mostrar una entidad en su propia pagina, para eso vamos a necesitar un template html con los valores del documento almacenado en la base de datos, un template es una buena herramienta para eso, veamos como hacerlo en nuestra couchapp


primero creamos la funcion show, esta funcion es llamada para mostrar un documento en algun formato, en nuestro caso html


# generamos la funcion show
couchapp generate show dato
# la editamos con un editor de texto
vim shows/dato.js

en un principio vamos a ver este contenido



function(doc, req) {  
  
}

reemplazamos por algo parecido a esto



function(doc, req) {
    if (doc !== null && doc.name) {
        var ddoc = this,
            Mustache = require("vendor/couchapp/lib/mustache"),
            path = require("vendor/couchapp/lib/path").init(req),
            assetPath = path.asset();

        provides("html", function() {
            return Mustache.to_html(ddoc.templates.dato, {
                assetPath: assetPath,
                doc: doc
            });
        });
    }
    else {
        return "not found";
    }
}

en el codigo controlamos que tenemos un documento, sino mostramos "not found",
tambien creamos algunos objetos que nos ayudan y finalmente renderizamos el template pasandole algunos valores que seran usados dentro del template.


mi template esta en templates/dato.html (por eso ddoc.templates.dato) y contiene un template mustache, mira la documentacion de mustache para mas informacion sobre el formato


la url par acceder a esta funcion es [database]/_design/[app]/_show/[showname]/[docid] un ejemplo podria ser http://localhost:5984/datos/_design/datos/_show/dato/6bd97648d74961996c8f0d42b2005761

[EN] CouchApp IV: creating show functions with html templates

we need to display some entity in its own web page, for that we need to fill an html template with the values of the document stores in the database, a template is a nice fit for this, let's see how we do this in our couchapp


first, create the show function, this is a function that when called displays a document in some format, in our case in HTML


# generate the show function
couchapp generate show dato
# open it with a text editor
vim shows/dato.js

we will see this content



function(doc, req) {  
  
}

we replace the content with something like this



function(doc, req) {
    if (doc !== null && doc.name) {
        var ddoc = this,
            Mustache = require("vendor/couchapp/lib/mustache"),
            path = require("vendor/couchapp/lib/path").init(req),
            assetPath = path.asset();

        provides("html", function() {
            return Mustache.to_html(ddoc.templates.dato, {
                assetPath: assetPath,
                doc: doc
            });
        });
    }
    else {
        return "not found";
    }
}

here we check that we have a document, if not return "not found", also we create some objects that will help us and finally we render the template passing some values that will be used in the template.


my template is located at templates/dato.html (that's why ddoc.templates.dato) and contains a mustache template, see mustache documentation for information about the format


the url to access this function is [database]/_design/[app]/_show/[showname]/[docid] an example could be http://localhost:5984/datos/_design/datos/_show/dato/6bd97648d74961996c8f0d42b2005761

Seguidores

Archivo del Blog