{"id":15493447,"url":"https://github.com/sinclairzx81/magnum","last_synced_at":"2025-04-22T19:49:33.711Z","repository":{"id":12755612,"uuid":"15428872","full_name":"sinclairzx81/magnum","owner":"sinclairzx81","description":"general purpose template engine for nodejs.","archived":false,"fork":false,"pushed_at":"2013-12-28T05:56:16.000Z","size":236,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T16:13:48.321Z","etag":null,"topics":["node","razor","templates","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sinclairzx81.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-12-25T03:28:40.000Z","updated_at":"2017-09-25T19:18:22.000Z","dependencies_parsed_at":"2022-08-28T03:21:48.098Z","dependency_job_id":null,"html_url":"https://github.com/sinclairzx81/magnum","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fmagnum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fmagnum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fmagnum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sinclairzx81%2Fmagnum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sinclairzx81","download_url":"https://codeload.github.com/sinclairzx81/magnum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250313195,"owners_count":21410178,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["node","razor","templates","typescript"],"created_at":"2024-10-02T08:06:46.931Z","updated_at":"2025-04-22T19:49:33.647Z","avatar_url":"https://github.com/sinclairzx81.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿![](https://raw.github.com/sinclairzx81/magnum/master/logo.png)\n\nA fast, easy to use, general purpose template view engine for nodejs.\n\n### install\n\n\tnpm install magnum\n\n### contents\n* [overview](#overview)\n* [example](#example)\n* [api](#api)\n\t* [render](#render)\n\t* [compile](#compile)\n\t* [context](#context)\n* [syntax](#syntax)\n\t* [expressions](#expressions)\n\t* [if statements](#if)\n\t* [for statements](#for)\n\t* [comments](#commentblock)\n\t* [code blocks](#codeblock)\n* [layouts](#layouts)\n\t* [import](#import)\n\t* [render](#render)\n* [express](#express)\n\n\u003ca name='overview' /\u003e\n## overview\n\nMagnum is a general purpose logic driven templating engine for nodejs developers. Magnum templates allow developers to easily script \ncomplex view logic with a javascript like syntax. Magnum focuses on being general purpose to enable developers can leverage it for \ncode generation, html templates, xml or other niche template scenarios.\n\nInspired by Microsoft Razor\n\n\u003ca name='example' /\u003e\n## example\n\nThe following is a quick example demonstrating rendering a template.\n\n#### layout.html\n```html\n\n\u003chtml\u003e\n\n\t\u003chead\u003e\n\n\t\t@section header\n\n\t\u003c/head\u003e\n\n\t\u003cbody\u003e\n\n\t\t@section body\n\n\t\u003c/body\u003e\n\n\u003c/html\u003e\n```\n\n#### view.html\n```html\n\n@import 'layout.html'\n\n@section header {\n\n\t\u003ctitle\u003e@(context.title)\u003c/html\u003e\n}\n\n@section body {\n\n\t\u003ch1\u003eWelcome\u003c/h1\u003e\n}\n```\n\n#### app.js\n```javascript\nvar magnum = require('magnum')\n\nvar context = { title: 'my page'}\n\nvar html = magnum.render('./view.html', context)\n\nconsole.log(html)\n```\n\n#### outputs\n\n```html\n\u003chtml\u003e\n\n\t\u003chead\u003e\n\n\t\t\u003ctitle\u003emy page\u003c/html\u003e\n\n\t\u003c/head\u003e\n\n\t\u003cbody\u003e\n\n\t\t\u003ch1\u003eWelcome\u003c/h1\u003e\n\n\t\u003c/body\u003e\n\n\u003c/html\u003e\n```\n\n\u003ca name='api' /\u003e\n## api\n\nThe following outlines magnums methods.\n\n\u003ca name='compile' /\u003e\n### compile\n\nThe compile() method compiles the template file and returns a template object. \n\n```javascript\n\nvar magnum   = require('magnum')\n\nvar template = magnum.compile('./view.html')  // store for later\n\n//...later\n\nvar context = {title: 'my page'}\n\nvar html     = template.render(context) // render \n\nconsole.log(html)\n```\n\n\u003ca name='render' /\u003e\n### render\n\nTo quickly compile and render a view, call the magnum.render() method.\n\n```javascript\nvar magnum = require('magnum')\n\nvar output = magnum.render('./view.html')\n\n```\n\n### context\n\nWhen calling render() on a template (or via magnum itself), you can optionally pass a data context object to be rendered. \nMagnum encapulates all data passed on the \"context\"\nobject which is passed to magnum template on the render() method. Consider the following..\n\n#### template.html\n```html\n\n\u003cp\u003eHi @(context.name)\u003c/p\u003e\n\n\u003cul\u003e\n\n\t@for(var i = 0; i \u003c context.fruits.length; i++) {\n\n\t\t\u003cli\u003e@(context.fruits[i])\u003c/li\u003e\n\t}\n\n\u003c/ul\u003e\n```\n\n#### app.js\n```javascript\n\nvar magnum   = require('magnum')\n\nvar context = {name   : 'dave', \n\t\t       fruits : ['apples', \n\t\t\t\t\t\t 'oranges', \n\t\t\t\t\t\t 'kiwifruit', \n\t\t\t\t\t\t 'mangos', \n\t\t\t\t\t\t 'grapes' ]}\n\nvar html = magnum.render('./template.html', context)\n```\n\nthe context can be accessed in the following way...\n\n\n\n\u003ca name='syntax' /\u003e\n## syntax\n\nThe following syntax is available inside magnum templates.\n\n\u003ca name='expressions' /\u003e\n### expressions\n\nThe expression syntax allows a user to emit the value within. The following are examples. \n\n```\n@* strings *@\n@('hello world')\n\n@* numbers *@\n@(123)\n\n@* conditions: displays false) *@\n@(10 \u003e 20)\n\n@* ternary: displays 'cat' *@\n@(true ? 'cat' : 'dog')\n\n@* variables *@\n@(myvariable)\n\n@* functions: displays 'hello world' *@\n@{ var message = function() { return 'hello world' } }\n\n@(message())\n\n```\n\n\u003ca name='if' /\u003e\n### if statement\n\nif statments are supported.\n\n```\n@if(expression) {\n\tsome content\n}\n\n@if(a \u003e 10) {\n\tsome content\n}\n\n@(user.loggedin) {\n\t\u003cspan\u003ewelcome\u003c/span\u003e\n}\n```\n\n\u003ca name='for' /\u003e\n### for statement\n\nthe following for loops are supported.\n\n```\n@for(var i = i; i \u003c 100; i++) {\n\t@(i)\n}\n\n@for(var n in list) {\n\t@(list[n])\n}\n```\n\n\n\u003ca name='codeblock' /\u003e\n### code\n\ncode blocks can be useful for adding template side rendering logic.\n\n```\n@{\n\tvar message = 'hello'\n}\n\n@(message)\n```\n\n\u003ca name='commentblock' /\u003e\n### comments\n```\n@*\n\tthis comment will not be rendered!\n*@\n```\n\n\u003ca name=\"template_layouts_and_sections\" /\u003e\n### layouts and sections\n\nMangum supports layouts and sections. This section describes how to use them.\n\n### import\n\nUse the import statement to have one template inheriate from another. This will allow the child template to (optionally) override the \nsections of the parent. \n\n#### layout.html\nlayout.html will be the parent template, here we define three sections.. header, body and footer. \n\n```html\n\u003chtml\u003e\n\n\t\u003chead\u003e\n\n\t\t@section header\n\n\t\u003c/head\u003e\n\n\t\u003cbody\u003e\n\n\t\t@section body\n\n\t\t@section footer {\n\n\t\t\t\u003cspan\u003ecopyright 2013\u003c/span\u003e\n\t\t}\n\t\u003c/body\u003e\n\n\u003c/html\u003e\n```\n\n#### view.html\nInside view.html, we inheriate from layout.html with the import keyword. Inside view.html, we define sections for header and body. Note that\nthe default content for the footer not overridden. If the child template does not override a parents section, the parents section will be used\ninstead.\n\n```html\n@import 'layout.html'\n\n@section header {\n\n\t\u003ctitle\u003e@(context.title)\u003c/html\u003e\n}\n\n@section body {\n\n\t\u003ch1\u003eWelcome\u003c/h1\u003e\n}\n```\n\n### render\n\nMagnum templates allow the user to render snippets of content in place. The following renders a template named navigation.html in place. \n\n#### navigation.html\n```html\n\n\t\u003cul\u003e\n\n\t\t\u003cli\u003e\u003ca href='#'\u003ehome\u003c/a\u003e\u003c/li\u003e\n\n\t\t\u003cli\u003e\u003ca href='#'\u003eabout\u003c/a\u003e\u003c/li\u003e\n\n\t\t\u003cli\u003e\u003ca href='#'\u003econtact\u003c/a\u003e\u003c/li\u003e\n\n\t\u003c/ul\u003e\n```\n\n#### layout.html\n```html\n\u003chtml\u003e\n\n\t\u003chead\u003e\n\n\t\u003c/head\u003e\n\n\t\u003cbody\u003e\n\n\t\t@render 'navigation.html'\n\n\t\t@section content\n\n\t\u003c/body\u003e\n\n\u003c/html\u003e\n```\n\n\u003ca name='express' /\u003e\n##express\nMagnum does not provide any built in middleware for specifically for express, however it is trivial for developers to 'snap in' utility \nmethods on the express response to acheive desireable results. consider the following...\n\n```javascript\n\nvar express = require('express')\n\nvar magnum  = require('magnum')\n\n//----------------------------------------------\n// setup: create and apply render method\n//----------------------------------------------\n\nvar app     = express()\n\napp.use(function (req, res, next) {\n\n    res.render = function (path, context) {\n\n        var output = magnum.render(path, context)\n\n        res.setHeader('Content-Type', 'text/html')\n\n        res.setHeader('Content-Length', Buffer.byteLength(output))\n\n        res.send(output)\n    }\n\t\n    next()\n})\n\n\n//----------------------------------------------\n// render the template...\n//----------------------------------------------\n\napp.get('/', function(req, res) {\n\t\n\tres.render('./index.html') \n})\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinclairzx81%2Fmagnum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsinclairzx81%2Fmagnum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsinclairzx81%2Fmagnum/lists"}