---
title: "NodeJS Marko custom tags Quick Reference"
author: "fairy_xzc"
date: "2017-01-20T14:35:37Z"
tags: []
categories: ["Web前端"]
slug: "nodejs-marko-custom-tags-quick-reference"
draft: false
lastmod: "2017-01-20T15:36:53Z"
id: 2354225
url: "https://agilestyle.iteye.com/blog/2354225"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2354225](/blog/2354225)**

 

## Prerequisite

[http://agilestyle.iteye.com/blog/2354215](/blog/2354215)

 

## Project Directory

![](http://dl2.iteye.com/upload/attachment/0122/7240/f130a2d2-6ac3-3c9e-a4fc-8d96ad28d458.png)

 

## SRC

marko-taglib.json

```
{
	"tag-dir": "./components"
}
```

 

src/components/app-hello/template.marko

```
<div>
	Hello ${data.name}! You have ${data.messageCount} message(s).
</div>
```

 

src/components/app-hello/index.js

```
var template = require('./template.marko');

module.exports = function(input, out) {
	var name = input.name;
	var messageCount = input.messageCount;

	if(name) {
		name = name.toUpperCase();
	} else {
		name = 'annoymous';
	}

	template.render({
		name: name,
		messageCount: messageCount
	}, out);
}
```

 

src/pages/home/template.marko

```
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Marko Demo</title>
</head>
<body>
	<h1>Marko Demo</h1>

	Hello ${data.name}

	<ul if(data.colors)>
        <li for(color in data.colors)>
            ${color}
        </li>
    </ul>
    <div else>
        No colors!
    </div>

    <app-hello name="Marko" message-count="30"/>
    <app-hello message-count="1"/>
</body>
</html>
```

 

## Run & Test

![](http://dl2.iteye.com/upload/attachment/0122/7242/6d6ce296-268f-3948-b386-f9b18a801276.png)  
 

## marko-tag.json

![](http://dl2.iteye.com/upload/attachment/0122/7264/d9ad44d4-367a-3992-b463-e94eea969750.png)  
 

src/components/app-hello目录下新建一个marko-tag.json文件

![](http://dl2.iteye.com/upload/attachment/0122/7268/636c1f2f-e57f-38cc-8dfc-72cbbbe7f7b5.png)  
 

src/pages/home/template.marko新增一行如下：

```
<app-hello invalid="error" />
```

 

启动Server，会直接报错如下：

![](http://dl2.iteye.com/upload/attachment/0122/7270/a0fac443-81aa-3e06-9980-3e4c591c12d5.png)  
 

## Reference

[https://www.youtube.com/watch?v=00xBpj89AkY](https://www.youtube.com/watch?v=00xBpj89AkY%20)