Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 渲染樱桃色';具有id/hash的s模板_Javascript_Python_Cherrypy - Fatal编程技术网

Javascript 渲染樱桃色';具有id/hash的s模板

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的部分是客户机的职

如何呈现页面并转到特定id

现在,我有一个下面的函数与此代码:

@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());
      });