在 CodeIgniter 中创建主体 ID
有没有办法在 codeigniter 中为不同页面创建动态主体 id,将空格转换为破折号?
我一直在论坛和搜索引擎上徘徊,但我没有运气。
这是我的代码:
<body id="{$template.title}">
这是输出:
<body id="About us">
我希望它是:
<body id="about-us">
让我知道谢谢
Is there a way to create a dynamic body id in codeigniter for different pages that converts spaces to dashes?
I have been around forums and search engines but I have no luck.
Here is my code:
<body id="{$template.title}">
Here is the output:
<body id="About us">
I want it to be:
<body id="about-us">
let me know Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您能解释一下为什么要这样做吗?如果您尝试使用 JavaScript 获取页面标题,则可以使用
document.title
轻松实现。如果你想有一个唯一的ID值,你可以把它放在一个隐藏的输入框中,id为“page_value”或类似的东西,或者你可以简单地将它放在一个全局javascript变量中。如果您想在 POST 请求中将页面的 id 值传递到下一页而不使用 flashdata,您可以将其放在名为“page_value”或其他名称的隐藏输入框中。如果你想在CodeIgniter中临时存储下一个页面加载的数据,你可以在sessions类中使用CI的flashdata:http://codeigniter.com/user_guide/libraries/sessions.html
但是,如果您确实想根据正文所在的页面为正文提供唯一的 id(尽管我认为这种方法是一种有点误导),您将不得不查看模板系统的插入方法。您可能可以在模板类中创建一个辅助标题,它采用标题值并删除空白字符并将其替换为破折号。像这样的事情:
Could you explain, perhaps, why you want to do this? If you're trying to get the title of the page in javascript, you can do so easily using
document.title
. If you want to have a unique ID value, you can either put it in a hidden input box with the id "page_value" or something like that, or you can simply put it in a global javascript variable. If you'd like to pass the id value of the page to the next page in a POST request without using flashdata, you can put it in a hidden input box with name "page_value" or something. If you want to temporarily store data for the next page load in CodeIgniter, you can use CI's flashdata in the sessions class:http://codeigniter.com/user_guide/libraries/sessions.html
But, if you do want to give the body a unique id depending on what page it's on (and even though I think this approach is a bit misguided), you will have to look at your template system's insertion methods. You could probably make a secondary title in your template class that takes the title value and strips out whitespace characters and replaces it with dashes. Something like this: