简体   繁体   English

ExpressJS&Swig:根据路线更改模板变量?

[英]ExpressJS & Swig: Changing template variables based on route?

I'm developing an app using MEAN.js, and I'm trying to fix the Articles (blog) so it's more friendly for social sharing. 我正在使用MEAN.js开发一个应用程序,我正在尝试修复文章(博客),因此它对社交分享更友好。 The problem I'm having is populating the open graph meta data so that it populates the og:url value with the actual url being shared. 我遇到的问题是填充打开的图元数据,以便填充og:url值并共享实际的url。 Here are what the files look like: 以下是文件的外观:

express.js express.js

// Setting application local variables, to be rendered in the template's variables in layout.server.view.html below.
app.locals.siteName = config.app.siteName;
app.locals.title = config.app.title;
app.locals.description = config.app.description;
app.locals.keywords = config.app.keywords;
app.locals.imageUrl = config.app.imageUrl;
app.locals.facebookAppId = config.facebook.clientID;
app.locals.jsFiles = config.getJavaScriptAssets();
app.locals.cssFiles = config.getCSSAssets();

// Passing the request url to environment locals
app.use(function(req, res, next) {

    // NOTE: This is the line that's setting the url on the layout template.
    res.locals.url = req.protocol + '://' + req.headers.host + req.url;
    next();
});

layout.server.view.html layout.server.view.html

// This is the template being rendered, using Swig as the template engine.
// The variables (e.g. siteName, title, etc.) are being set using the app.locals and req.locals.url variables in the express.js file above.

<!-- Facebook META -->
<meta id="fb-app-id" property="fb:app_id" content="{{facebookAppId}}">
<meta id="fb-site-name" property="og:site_name" content="{{siteName}}">
<meta id="fb-title" property="og:title" content="{{title}}">
<meta id="fb-description" property="og:description" content="{{description}}">
<meta id="fb-url" property="og:url" content="{{url}}">
<meta id="fb-image" property="og:image" content="{{imageUrl}}">
<meta id="fb-type" property="og:type" content="website">

<!-- Twitter META -->
<meta id="twitter-title" name="twitter:title" content="{{title}}">
<meta id="twitter-description" name="twitter:description" content="{{description}}">
<meta id="twitter-url" name="twitter:url" content="{{url}}">
<meta id="twitter-image" name="twitter:image" content="{{imageUrl}}">

articles.server.controller.js articles.server.controller.js

// Accessed only when a user visits one of the 'Articles' (blog) pages.
// When a user visits a url for an Article, this file grabs the article
// from the database and updates the local variables with article-specific
// values. This works for everything EXCEPT the url variable.

exports.articleByUrl = function(req, res, next, id) {
    var url = req.params.articleUrl;
    Article.findOne({urlTitle: url}).populate('user', 'displayName').exec(function(err, article) {
        if (err) return next(err);
        if (!article) return next(new Error('Failed to load article ' + id + ' with friendly URL "' + url + '".'));
        req.app.locals.title = article.title;
        req.app.locals.description = article.summary;

        // This is the line that I'm HOPING will update the url variable in
        // the template file above, but it's not working. I've also tried
        // using req.app.locals.url, but that didn't work either.
        res.locals.url = req.protocol + '://' + req.get('host') + '/#!' + req.originalUrl;

        req.app.locals.imageUrl = article.img;
        req.article = article;
        next();
    });
};

Currently, all of the values populate correctly EXCEPT the url value - it always shows the root url (ie http://localhost:3000 on my dev machine). 目前,除了url值之外,所有值都正确填充 - 它总是显示根URL(即我的开发机器上的http:// localhost:3000 )。 Any help, or smarter ways to do what I want would be REALLY appreciated. 任何帮助,或更聪明的方式来做我想要的将非常感激。 I've spent 2 days on this, and my stubbornness is finally giving out. 我花了两天时间,我的顽固终于放弃了。

When you use: 当你使用:

req.app.locals

It changes all the values for the app for everyone (until server restart). 它会为每个人更改应用程序的所有值(直到服务器重新启动)。 The proper way is to use: 正确的方法是使用:

res.app.locals

BUT when you do, it only does it for the initial page request and doesn't persist when the hash change is applied on the client side. 但是,当您这样做时,它只对初始页面请求执行此操作,并且在客户端应用哈希更改时不会保留。

The recommended approach would be to create a new server layout file with the URL in question so there is no hash change, then perhaps your navigation for the new server template file can link back to the URLs in you one-page app (with the original server template). 建议的方法是使用相关URL创建一个新的服务器布局文件,这样就不会更改哈希值,那么新服务器模板文件的导航可能会链接回您的单页应用程序中的URL(与原始服务器模板)。

Best of luck. 祝你好运。 😀 😀

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM