Express和Handlebars:实施多个主题

编程入门 行业动态 更新时间:2024-10-23 14:19:25
本文介绍了Express和Handlebars:实施多个主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我和团队的任务是为我们的项目实现多个主题(请记住,我们现在有一个主题项目).

I and the team got a task to implement multiple themes for our project (keeping in mind that we have a single-theme project right now).

通过主题,我并不是指 CSS ,而是指不同的标记文件,某些功能的某些特征等等.(如果您不喜欢这个词,在这种情况下,主题"不使用它,我希望您能理解:某些事情需要在整个代码库中重用,某些事情需要特定于 theme .总体而言,我们没有以可维护的方式进行结构化并实现大部分功能的问题,但是我们不知道如何制作多个 express-handlebars 配置,我们可以根据进行切换>某些因素.

By theme I don't mean just CSS, I mean different markup files, certain features for certain things, etc. (If you don't like the word 'theme' in this context don't use it, I hope you get the idea: some things need to be reused across the codebase, some things need to be specific to a theme). Overall, we have no problem of structuring it in a maintainable way and implementing the most of it, but we don't know how to make multiple express-handlebars configs, which we can switch based on certain factors.

现在,我们有一个非常标准的 express-handlebars 项目结构,如下所示:

Right now we have a pretty standard express-handlebars project structure like such:

views/ index layouts/ main.hbs content.hbs whatever.hbs

我们想要的是这样的

views/ themes theme1 layouts/ main.hbs index.hbs content.hbs theme2 layouts/ main.hbs index

我们使用一种非常标准的方法在应用启动文件中初始化 express-handlebars ,如下所示:

We use a pretty standard way to initialize express-handlebars in our app startup file like this:

var handlebars = exphbs.create({ defaultLayout: 'main', extname: '.hbs', helpers: require('./server/hbsHelpers') }); app.engine('.hbs', handlebars.engine); app.set('view engine', '.hbs'); app.set('views', __dirname + '/views');

我当时正在考虑初始化几个 exphbs 对象,并在运行时(请求时间)切换它们,但这无法正常工作,因为app.set是整个应用程序的设置(长话短说,我们使用一个应用程序托管多个网站,并且无法在应用程序级别(仅在明确请求级别)进行任何更改).

I was thinking to initialize several exphbs objects and switch them at runtime (request time) but this is not going to work since app.set would be an app-wide setting (long story short we host multiple websites with one app and can't afford change anything on app level, only on the express request level).

期望的行为是这样的:

res.render(‘/theme1/file.hbs’);

看起来没有配置就可以直接呈现没有问题,但是我们还需要指定车把需要从特定来源获取特定主题的布局和局部

looks like there's no problem just to render directly without a configuration, but then we also need to specify that handlebars needs to grab layouts and partials for a specific theme from a specific source

res.render(‘file.hbs’, { theme: ‘theme1’ });

我们正在使用这种快速车把的变体– github/ericf/express-车把 (也许您可以建议一种替代方法,使我上面描述的内容可以互换并且不会破坏很多东西-我们的代码库非常大)?

We're using this variant of express handlebars – github/ericf/express-handlebars (May be you can advice an alternative that allows what I described above that we can interchange and not break a lot of things - our code base is pretty huge)?

我们将不胜感激您的任何帮助,想法或建议.

Any of your help, ideas or advice would be highly appreciated.

推荐答案

您没有提到如何确定每个请求将呈现哪个主题,但是我认为最简单的方法是覆盖res.render()方法. /p>

You didn't mention how you would determine which theme each request would be rendered with but I think the simplest approach is to override the res.render() method.

app.use(function(req, res, next) { // cache original render var _render = res.render; res.render = function(view, options, done) { // custom logic to determine which theme to render var theme = getThemeFromRequest(req); // ends up rendering /themes/theme1/index.hbs _render.call(this, 'themes/' + theme + '/' + view, options, done); }; next(); }); function getThemeFromRequest(req) { // in your case you probably would get this from req.hostname or something // but this example will render the file from theme2 if you add ?theme=2 to the url if(req.query && req.query.theme) { return 'theme' + req.query.theme; } // default to theme1 return 'theme1'; }

关于这一点的好处是,您在控制器中的调用仍将保持干净-res.render('feature/index.hbs')只要您在每个主题文件夹中都具有该文件,您就可以了.

The nice thing about this is your calls in your controller will still be clean - res.render('feature/index.hbs') as long as you have that file in each theme folder you should be good.

您可以使getThemeFromRequest更加智能,并且可以检查该主题的模板是否存在,如果不存在,则使用默认主题呈现文件,这可能有助于防止一堆重复的html.

You could make getThemeFromRequest a lot smarter and it could check to see if the template exists for that theme and if not render the file from the default theme which may help prevent a bunch of duplicate html.

更多推荐

Express和Handlebars:实施多个主题

本文发布于:2023-07-17 03:35:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1129009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   主题   Express   Handlebars

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!