- 论坛徽章:
- 0
|
30可用积分
最近写了一些根据基本数据结构生成代码的代码,不知道还有没有更好的算法
1. 嵌套结构
my $data = { title => { content => 'This is an Title' }};
生成字符串
<title>This is an Title</title>
2. 属性结构
my $data = { src => 'w3school.jpg', width => 104, height => 142};
生成字符串
<tag src='w3school.jpg' width='104' height='142' />
3. 平行结构
$data = {
h1 => { content => 'This is h1' },
h2 => { content => 'This is h2' },
h3 => { content => 'This is h3' },
};
生成字符串
<h1>This is h1</h1>
<h2>This is h2</h2>
<h3>This is h3</h3>
另外还有一些组合的结构:
$data = {
href => 'www.w3school.com',
target => '_blank',
content => 'Visit W3Schools!',
};
$data = {
id => 'header',
style => 'background-color:#FFA500;',
h1 => {
style => 'margin-bottom:0;',
content => 'Main Title of web',
},
};
等等,都是由前三种结构演变而来 |
|