简体   繁体   English

从控制器传递样式表到视图

[英]Passing Stylesheet to View from Controller

Is it possible to pass a Stylesheet to a view from a controller? 是否可以将样式表从控制器传递给视图?

I have a master.layout file. 我有一个master.layout文件。 This master layout is used for every page on the site. 该主版面用于网站上的每个页面。 On the site the user can change colors and properties of their user profile, so on the profile pages I need to pass in either a custom user stylesheet, or custom style rules. 用户可以在网站上更改其用户配置文件的颜色和属性,因此在配置文件页面上,我需要传递自定义用户样式表或自定义样式规则。

The only solution I have found is to include an if statement in the view and if the page is a profile page then check if $style is defined, if so include it. 我发现的唯一解决方案是在视图中包含if语句,并且该页面是否为配置文件页面,然后检查是否定义了$style ,如果包含,则将其包括在内。 But this approach seems clunky. 但是这种方法似乎很笨拙。

Is it possible to define the layout and pass in scripts or style rules from within the controller itself? 是否可以在控制器本身内部定义布局并传入脚本或样式规则?

Edit: 编辑:

The style rules are stored in a database in columns such as profile.background_color and profile.heading_color . 样式规则存储在数据库中的profile.background_colorprofile.heading_color列中。 The controller then constructs them into either a stylesheet, and caches it, or includes the styles directly within a <style> tag on the profile view. 然后,控制器将它们构造到样式表中并进行缓存,或者将样式直接包含在配置文件视图的<style>标记中。 I havn't decided which way to do it yet, but both are possible. 我尚未决定采用哪种方法,但是两者都有可能。

The advice about Facade was weird. 关于Facade的建议很奇怪。 That doens't really help you here. 那对你真的没有帮助。

You'll want to do a few things. 您需要做一些事情。

In your pages controller you can add: 在页面控制器中,您可以添加:

$layout->with('extra_stylesheets', ['pages/someslug.css']);

Then that .css file should be processed as PHP using a little mod_rewrite and htaccess fun so it can be cached heavily (use more .htaccess for this, Google will help). 然后,应该使用一点mod_rewrite和htaccess的乐趣将该.css文件作为PHP进行处理,以便可以对其进行大量缓存(为此使用更多.htaccess,Google会有所帮助)。

The layout then is the only location that needs to check for extra stylesheets and your pages view has a little less logic in it and your output HTML doesn't have a bunch of nasty <style> tags. 然后,布局是唯一需要检查额外样式表的位置,并且页面视图中的逻辑少了一点,并且输出HTML中没有一堆讨厌的<style>标签。

In master.layout , ensure you have a @section('userStyle')...@stop or similar. master.layout ,确保您具有@section('userStyle')...@stop或类似名称。

In your controller, set the property protected $layout = 'master.layout'; 在您的控制器中,设置属性protected $layout = 'master.layout'; .

Build out the css as you are currently doing and store it in a php variable $css . 按照当前的方式构建CSS,并将其存储在php变量$css

Then you can load up the css into your layout with $this->layout->userStyle = $css . 然后,您可以使用$this->layout->userStyle = $css将CSS加载到布局中。

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

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