doushang2023 2017-09-14 03:54
浏览 38

设计SilverStripe博客模块的最佳方法

I am trying to add CSS requirements to the Blog module.

The current approach I have adopted is creating a subclass of the Blog class and Blog_Controller (as well as the BlogPost and BlogPost_Controller) in an attempt to introduce css requirements into the init() function. However, for some reason, I can’t seem to get any reference to these css files to work.

  class ProjectsHolder extends Blog{

    private static $allowed_children = array(
       'ProjectPage'
     );
  }

  class ProjectsHolder_Controller extends Blog_Controller{

    public function init(){
      parent::init();
      Requirements::css("{$this->ThemeDir()}/css/projects.css");
    }
  }

I have also tried the <% require css %> template syntax. The only way I can get the css to render is by hardcoding the css link tag inside a head tag at the top of the template (which is terrible).

I also noticed SilverStripe’s DataExtension/SiteConfig approach for injecting functionality into classes, which looks like it might be applicable to my situation.

My question is: what is the best practice for styling modules such as the Blog module.

There is clearly something I am missing or not understanding here; any clarification would be much appreciated!

  • 写回答

1条回答 默认 最新

  • dragon4808 2017-09-14 20:41
    关注

    At first glance, if you are using a Themed CSS, then you can simply use

    Requirements::themedCSS("projects");

    In your code though, maybe the brackets are causing the issue: {$this->ThemeDir()} ?

    The SilverStripe Docs give examples of doing this in both the template and the php files. It has recently been brought to my attention, though, that the 'preferred' way these days is to still use the 'requirements' but in the template like you mentioned.

    <% require themedCSS("projects") %>

    As for just styling your Blog module, and not having to extend anything or modify any of it's core code, why aren't you just adding the styles to your main style sheet or to it's own file/css and just include them in the main theme template?

    What I do, is create a file called blog.css and just include it in my themes/themename/templates/Page.ss file using the above <% require %> for templates. Surely your css files don't have that large of a footprint that you need to take advantage of the Combining Files.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行