Javascript 渲染樱桃色';具有id/hash的s模板
如何呈现页面并转到特定id 现在,我有一个下面的函数与此代码:Javascript 渲染樱桃色';具有id/hash的s模板,javascript,python,cherrypy,Javascript,Python,Cherrypy,如何呈现页面并转到特定id 现在,我有一个下面的函数与此代码: @cherrypy.expose @require def page(): tmpl = lookup.get_template('page.html') return tmpl.render() 然而,现在page.html确实有几个子页面,我可以通过类似mydomain.com/page\someid的URL访问这些子页面 有没有一种方法可以呈现直接指向id的模板?我认为您是在混淆视听,URL的部分是客户机的职
@cherrypy.expose
@require
def page():
tmpl = lookup.get_template('page.html')
return tmpl.render()
然而,现在page.html
确实有几个子页面,我可以通过类似mydomain.com/page\someid
的URL访问这些子页面
有没有一种方法可以呈现直接指向id的模板?我认为您是在混淆视听,URL的
部分是客户机的职责,需要关注特定的元素id。不过,我认为您希望通过javascript动态嵌入页面特定部分的块,我可以考虑两种可能性:
首先,使用不同子模板的不同ID组成完整页面模板,如果您使用模板模块(如)并使用cherrypy处理程序返回indivudual部分,这很容易。当然,这是假设您控制页面的内容,并且ID不是动态的(由db或其他东西生成)主站点是一堆包含的
@cherrypy.expose
def page_part(section):
tpl_name = 'page_%s.html' % section
# validate that the template exists, then:
tmpl = lookup.get_template(tpl_name)
return tmpl.render()
Mako模板:
page.html:
<html>
<body>
<div id="main">
This is the main content of the site!
</div>
<h4>Sections</h4>
<div id="section_1">
<%include file="page_section1.html" />
</div>
<div id="section_2">
<%include file="page_section2.html" />
</div>
</body>
</html>
URL上的#id
有什么问题?。你能提供一点关于你为什么要这么做的背景吗?#id
没有错,我只是不知道怎么做,因为我正在访问模板,它是一个.html
文件,我不能做类似查找.get_模板('page.html#id')
的事情。我不知道如何将#id
放入tmpl.render()
。事实上,我使用的是mako
。我会调查的。事先谢谢。你能详细解释一下第一种使用mako
的方法是如何工作的吗?如果我想加载我的page.html
,我该如何附加页面id,例如sectOne
?@cherrun我希望答案的改进能澄清这个想法。抱歉,但我不知道这是怎么回事。当我运行tpl\u name='page\u%s.html.%section
然后返回tpl.render()
时,我肯定只会得到那个特定的文件,例如page\u section1.html
文件,而不是整个page.html
文件,然后跳转到section1
。基本上,如果不使用javascript或在URL中指定,您无法控制客户端如何聚焦到特定元素。在js中,您可以执行document.location='#mi_elem_id'
,这将集中于元素。
<p> Content of section 1</p>
<p> Content of section 2</p>
$.get('/page.html',
function(data){
$('#this_page_id').html($('#sect_in_other_page', $(data)).html());
});