Category: 小小草

IT 技术领域学海无涯。其实任何领域都学海无涯,无非 IT 发展太快了,让我有更多嘘唏。希望我掌握的技术有如小小草,虽然渺小,却有旺盛的生命力。

  • Double quotes or single quotes inside html tag, which one is the better?

    If you google “Double quotes or single quotes inside html tag, which is better”, you might hear someone is talking about using double quotes in html won’t pass XHMTL validation. I think it is nonsense.

    Both of these characters are valid for XHTML, i.e. both

    <tag att="value">
    

    and

    <tag att='value' >
    

    are valid. However, I have a good reason to encourage you to stick to double quotes.

    When value is taken from user input, it is a must to convert all applicable characters to HTML entities by htmlentities(), and htmlentities() default flag is ENT_COMPAT which convert double quotes but leave single quotes alone. That is to say,

    If I use

    <tag att="(value taken from user input)">
    

    constantly in templates, and if htmlentities() converts all double quotes in user input, whatever user input is, it will not break html output.

    I used to use double quotes and single quotes interchangeably, but I learned from Magento. Magento sticks to using double quotes. Its htmlEscape() bridges to htmlspecialchars() but it does not let you pass in a flag to control how to convert double quotes or single quotes. So, if you use

    <tag att='(value taken from user input)'>
    

    , and if you rely on Magento htmlEscape() to convert special charcters for you, when user input has some single quotes, it ruins html output. I am sure you do not want it happens, so stick to using double quotes, always.

  • Which part of MVC do Magento blocks belong to?

    Someone said Magento was not strictly MVC structured. I am intended to accept this point. But MVC does give people an easy way to understand how system works. So if I regard Magento as an MVC system, which part of MVC do Magento blocks fall into?

    I used to think blocks were Views because they were tied with templates. As time goes, now I think blocks are Models because blocks implement a lot of business logic as Magento model classes. Today I have another reason to support blocks are Models:

    When a frontend_model is initialised for a Sytem Configuration field or an EAV attribute field, it is actually a block class.

  • Lack of ideas to manage javascript in Magento

    In Magento System >> Configuration >> Catalog >> Frontend, when List Mode selected value is changed, some fields (i.e. Products per page on Grid allowed values, Products per Page on Grid default value, Products per Page on List allowed values, Products per Page on List default value) are shown/hidden accordingly.

    List mode showing both grid and list options
    List mode showing both grid and list options

    List mode showing only grid options
    List mode showing only grid options

    I had thought List Mode had a special frontend model defined so it could interact with other fields. I spent an hour to search for special settings of List Mode but did not find any. Instead I was disappointed to find out the frondend interaction is not defined in xml or php class, but by app/design/adminhtml/default/default/template/system/config/js.phtml.

    The way Magento did this kind of interaction is not replicable, which is a pity. By and large, the way how Magento uses javascript is messy. Maybe I am asking too much from Magento. What I am keen on is a “Google Web Toolkit” way, that means no need to manage javascript at all because javascript is compiled from server end script.

  • Constants are not overridden as varibles

    今天发现我写的一个底层的 Magento class 竟然有个低级错误。错误的根源是我在父类里定义了一个常量

    const XML_PATH_TO_SYSTEM_CONFIG = 'some value';
    

    然后在父类里有个方法用到了这个常量

    public static function getConfigValue()
    {
    	return Mage::getStoreConfig(self::XML_PATH_TO_SYSTEM_CONFIG);
    }
    

    然后在子类里复写了常量

    const XML_PATH_TO_SYSTEM_CONFIG = 'some other value';
    

    但子类并没有父类的方法 getConfigValue()。我指望通过复写每个子类的常量后调用子类的方法 getConfigValue() 得到不同的值,哪知道每次都得到跟父类的方法 getConfigValue() 相同的值。

    仔细一想,这是因为父类的 self::XML_PATH_TO_SYSTEM_CONFIG 并没有被复写。改用一个变量来存储值就能达到我的原定目的,但这个变量不能是静态变量,否则效果还是跟常量一样。

  • Javascript bookmark this page in Chrome

    I have a stupid client asked me to add “Bookmark this page” to his web pages. I explained to him how bad usability it is but he did not listen to me. He insisted the majority of web users were as stupid as him, so him wanted to help them bookmark the page.

    Nevertheless, it is not my site and he is my customer. Customer is the God. So I spent some time digging around and found this script.

    <script type="text/javascript">
    //<![CDATA[
    function bookmark_us(){
    var url = window.location;
    var title = document.title;
    
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    } 
    else if(document.all)// ie
        window.external.AddFavorite(url, title);
    }
    //]]>
    </script>
    

    However, Chrome browser does not allow us to bookmark any page using javascript. There is no workaround, and I think Google is absolutely right on this.

    My concern is: the above script does not do anything in Chrome. Sooner or later my client will come back to me asking why my script does not work in Chrome. To avoid being questioned, I finally use another solution. It is a jQuery plugin called jBrowserBookmark. It will prompt the correct way to bookmark in unsupported browsers.

  • Lack of EAV framework for php

    最近我在仔细研究 Magento EAV 的机理。

    Magento 的强大跟 EAV module 的强大有很大关系,花点时间去研究很值得。我还发现 Magento EAV module 中一些不足之处,但我无力改进。于是想借鉴一下其他的 EAV framework 是怎么实现的,悲惨的是,如果说到 EAV php implementation,似乎 Magento EAV 就是唯一一个成熟的开源方案。但 EAV java implementation 倒有一些独立的选择。

    这是不是暗示 php 先天不足?尽管 Magento 让 php 在电子商务领域出尽风头,但这很可能是 php 的极致了。

  • Improve Magento entity setup

    Magento 文档鼓励用 entity setup (Mage_Eav_Model_Entity_Setup) 的方式去定义实体。这个类很强大,但我觉得不够灵活,因为无法用它精确控制 attribute, attribute group 和 attribute set。

    Attribute group 和 attribute set 是在两个维度分组 attribute,这样来组织 attribute 基本够用。但 setup 这个类添加 attribute 时,有不够周到的逻辑:

    如果 $attr[‘user_defined’] == 1 && empty($attr[‘group’]),那么这个 attribute 不会被添加到任何 attribute group 和 attribute set。我觉得更好的逻辑是添加到 general attribute group。

    如果 empty($attr[‘user_defined’]) && !empty($attr[‘group’]),那么 $attr[‘group’] 会被忽略,attribute 总是被添加到 general attribute group。

    没办法,已经这样啦,提供 install entities array 时,就避免进入这两种逻辑吧。

    如果我来重写 setup 类里的 addAttribute 方法,我会:

    1. 忽略 $attr[‘user_defined’] 对逻辑的影响。一个 attribute 的 user_defined 值只决定是否允许用户用 GUI 修改该 attribute。
    2. 增加对 $attr[‘attribute_set_name’] 的检查。如果 !empty($attr[‘attribute_set_name’]),则添加该 attribute 进相应的 attribute set 或 attribute sets。现有逻辑是把 attribute 一股脑地添加进所有的 attribute sets。
  • Dataflow profile back to work in Magento 1.5.0.0

    Magento 1.5.0.0 目前还是 alpha2 版,因为安装到第二步就有一个很明显的 bug,本来想等等再用的。

    可是 1.4.2.0 正式版 dataflow profile 跑不起来,一点 “Run Profile in Popup”,出来的 popup 就是空白。我很喜欢用外部编辑器格式化好数据,然后用 dataflow import 到 Magento 数据库。我有很多 modules 也用到 dataflow 来完成初始化,如果 dataflow 跑不起来,会给我造成很大的麻烦。

    联想到 Magento 1.5.0.0 在 release note 里说到它对 dataflow 作了改进,我想很可能修正了 1.4.2.0 带来的 bug。两害取其轻,所以我跳过 Magento 1.5.0.0 的全新安装过程,用 1.4.2.0 的数据库让 Magento 1.5.0.0 跑了起来。果然如我所料,dataflow profile 在 1.5.0.0 下工作正常。

    Dataflow profile back to work in Magento 1.5.0.0
    Dataflow profile back to work in Magento 1.5.0.0
  • SMPlayer can disable screensaver

    刚在 Fedora 14 下用 SMPlayer 看了第一场电影,发现 SMPlayer (当前版本 0.6.9)之前版本恼人的无法阻止进入屏保的 bug 不见了。很开心,电影也好看。

    Linux software 一直在进步。

  • Too early to try Magento 1.5.0.0 alpha2

    Magento 1.5.0.0 alpha2 出来了,我下载来尝鲜。可是,安装到第二步就进行不下去了。提示是:

    There was a problem proceeding with Magento installation.

    Please contact developers with error messages on this page.

    Notice: Undefined variable: params in /path/to/magento/app/code/core/Mage/Core/Block/Html/Select.php on line 142

    这挺令人失望的。