import arsd.dom;
string toD(string s) {
return `append(` ~ "`" ~ s ~ "`" ~ `);`;
}
template loadTemplateMixin(string doc) {
string helper() {
Document document = new Document;
document.parseSawAspCode = (string) => true;
document.parseStrict(doc);
string code;
void expand(Element element) {
if(auto asp = cast(AspCode) element) {
if(asp.source.length > 3 && asp.source[1] == '=')
code ~= `append(` ~ asp.source[2 .. $-1] ~ `);`;
else
code ~= asp.source[1 .. $-1];
} else if(auto tn = cast(TextNode) element) {
code ~= toD(tn.toString());
} else if(auto sn = cast(SpecialElement) element) {
code ~= toD(sn.toString());
} else {
code ~= toD("<" ~ element.tagName);
foreach(k, v; element.attributes) {
code ~= toD(" ");
code ~= toD(k.htmlEntitiesEncode);
code ~= toD("=\"");
code ~= toD(v.htmlEntitiesEncode);
code ~= toD("\"");
}
code ~= toD(">");
foreach(child; element.children)
expand(child);
code ~= toD("</" ~ element.tagName ~ ">");
}
}
expand(document.root);
return code;
}
enum loadTemplateMixin = helper();
}
// 用法
//也可这样:import("file.html")导入
enum doc = `<html><script> foo</script><style>css</style><test id="main"><%= my_string[0 .. 5] %></test>
<span>foo</span>
<span>foo</span>
<span>foo</span>
<% foreach(item; strings)
append(item);
%>
</html>`;
void main() {
string html;
//在<% %> 块中可看见.
string my_string = "你好.世界";
string[] strings = ["omg", "wtf", "lol"];
void append(string s) {
html ~= s;
}
mixin(loadTemplateMixin!doc);
import std.stdio;
writeln(html);
}
亚当嵌入d代码
原创
©著作权归作者所有:来自51CTO博客作者fqbqrr的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:ctrl+alt两键翻译网页
下一篇:d的core.反射示例
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
分享一款嵌入式开源按键框架代码工程MultiButton
分享一款嵌入式开源按键框架代码工程MultiButton
STM32 单片机 嵌入式 C语言 数据结构 -
LLM中的嵌入技术
LLM中的嵌入技术详解。
语言模型 嵌入技术 向量表示 AIGC的底层技术 -
亚当2014年的演讲
亚当2014年的演讲。
编译器 数组 数据 内联汇编 演讲 -
unity3d嵌入python unity3d如何嵌入网页
1.首先你要先下载一个插件:Embedded Browser
System html .net -
js 嵌入java代码 嵌入javascript
1.直接调用 使用<script>和</script>标记在需要的位置编写javascript程序. 例: <p> <script language="javascript"> alert("Hi~~"); </script> </p>2.事件调用 通
JavaScript Java HTML ViewUI javascript -
java主线程等待线程执行完
Java中线程分为两种类型:用户线程和守护线程。通过Thread.setDaemon(false)设置为用户线程;通过Thread.setDaemon(true)设置为守护线程。如果不设置次属性,默认为用户线程。用户线程和守护线程的区别:主线程结束后用户线程还会继续运行,JVM存活;主线程结束后守护线程和JVM的状态又下面第2条确定。如果没有用户线程,都是守护线程,那么JVM结束(随之而来的是所有
java主线程等待线程执行完 Java主线程与子线程的生死存亡 守护线程和用户线程的区别 守护线程 System