<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mariano Guerra's Log (Publicaciones sobre mustache)</title><link>http://marianoguerra.org/</link><description></description><atom:link href="http://marianoguerra.org/es/categories/mustache.xml" rel="self" type="application/rss+xml"></atom:link><language>es</language><lastBuildDate>Mon, 18 Nov 2024 17:56:21 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>CouchApp IV: creando funciones show con templates HTML</title><link>http://marianoguerra.org/es/posts/201010couchapp-iv-creando-funciones-show-con/</link><dc:creator>Mariano Guerra</dc:creator><description>&lt;p&gt;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&lt;/p&gt;&lt;br&gt;&lt;p&gt;primero creamos la funcion show, esta funcion es llamada para mostrar un documento en algun formato, en nuestro caso html&lt;/p&gt;&lt;br&gt;&lt;pre&gt;# generamos la funcion show&lt;br&gt;couchapp generate show dato&lt;br&gt;# la editamos con un editor de texto&lt;br&gt;vim shows/dato.js&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;p&gt;en un principio vamos a ver este contenido&lt;/p&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;function(doc, req) {  &lt;br&gt;&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;p&gt;reemplazamos por algo parecido a esto&lt;/p&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;function(doc, req) {&lt;br&gt;    if (doc !== null &amp;amp;&amp;amp; doc.name) {&lt;br&gt;        var ddoc = this,&lt;br&gt;            Mustache = require("vendor/couchapp/lib/mustache"),&lt;br&gt;            path = require("vendor/couchapp/lib/path").init(req),&lt;br&gt;            assetPath = path.asset();&lt;br&gt;&lt;br&gt;        provides("html", function() {&lt;br&gt;            return Mustache.to_html(ddoc.templates.dato, {&lt;br&gt;                assetPath: assetPath,&lt;br&gt;                doc: doc&lt;br&gt;            });&lt;br&gt;        });&lt;br&gt;    }&lt;br&gt;    else {&lt;br&gt;        return "not found";&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;p&gt;en el codigo controlamos que tenemos un documento, sino mostramos "not found", &lt;br&gt;tambien creamos algunos objetos que nos ayudan y finalmente renderizamos el template pasandole algunos valores que seran usados dentro del template.&lt;/p&gt;&lt;br&gt;&lt;p&gt;mi template esta en templates/dato.html (por eso ddoc.templates.dato) y contiene un template mustache, mira &lt;a href="http://mustache.github.com/"&gt;la documentacion de mustache&lt;/a&gt; para mas informacion sobre el formato&lt;/p&gt;&lt;br&gt;&lt;p&gt;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&lt;/p&gt;</description><guid>http://marianoguerra.org/es/posts/201010couchapp-iv-creando-funciones-show-con/</guid><pubDate>Fri, 22 Oct 2010 21:20:00 GMT</pubDate></item><item><title>[EN] CouchApp IV: creating show functions with html templates</title><link>http://marianoguerra.org/es/posts/201010en-couchapp-iv-creating-show-functions/</link><dc:creator>Mariano Guerra</dc:creator><description>&lt;p&gt;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&lt;/p&gt;&lt;br&gt;&lt;p&gt;first, create the show function, this is a function that when called displays a document in some format, in our case in HTML&lt;/p&gt;&lt;br&gt;&lt;pre&gt;# generate the show function&lt;br&gt;couchapp generate show dato&lt;br&gt;# open it with a text editor&lt;br&gt;vim shows/dato.js&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;p&gt;we will see this content&lt;/p&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;function(doc, req) {  &lt;br&gt;&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;p&gt;we replace the content with something like this&lt;/p&gt;&lt;br&gt;&lt;br&gt;&lt;pre&gt;function(doc, req) {&lt;br&gt;    if (doc !== null &amp;amp;&amp;amp; doc.name) {&lt;br&gt;        var ddoc = this,&lt;br&gt;            Mustache = require("vendor/couchapp/lib/mustache"),&lt;br&gt;            path = require("vendor/couchapp/lib/path").init(req),&lt;br&gt;            assetPath = path.asset();&lt;br&gt;&lt;br&gt;        provides("html", function() {&lt;br&gt;            return Mustache.to_html(ddoc.templates.dato, {&lt;br&gt;                assetPath: assetPath,&lt;br&gt;                doc: doc&lt;br&gt;            });&lt;br&gt;        });&lt;br&gt;    }&lt;br&gt;    else {&lt;br&gt;        return "not found";&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;p&gt;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.&lt;/p&gt;&lt;br&gt;&lt;p&gt;my template is located at templates/dato.html (that's why ddoc.templates.dato) and contains a mustache template, see &lt;a href="http://marianoguerra.org/es/posts/201010en-couchapp-iv-creating-show-functions/%22http:/mustache.github.com/%22"&gt;mustache documentation for information about the format&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;p&gt;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&lt;/p&gt;</description><guid>http://marianoguerra.org/es/posts/201010en-couchapp-iv-creating-show-functions/</guid><pubDate>Fri, 22 Oct 2010 21:15:00 GMT</pubDate></item></channel></rss>