T
T
Test Test2015-04-15 14:07:45
Yii
Test Test, 2015-04-15 14:07:45

Yii: How to extend and override a method in CHtml?

Good afternoon! Such a question, there is one class in Yii, it is called CHtml . In particular, I'm interested in the scriptFile method , which generates the html code with the link. of that and that file.
I don't want the tag to be created with type="text/javascript" , and I don't want to make edits in the framework.
Is it possible to somehow extend the CHtml class , and its scriptFile method , so that when CHtml::scriptFIle() is called, my extended method is called?
Method code:

public static function scriptFile($url,array $htmlOptions=array())
{
  $defaultHtmlOptions=array(
    'type'=>'text/javascript',
    'src'=>$url
  );
  $htmlOptions=array_merge($defaultHtmlOptions,$htmlOptions);
  return self::tag('script',$htmlOptions,'');
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2015-04-15
@AloneCoder

and what's the problem?

class MyHtml extends CHtml {
    public static function scriptFile($url,array $htmlOptions=array())
    {
        $defaultHtmlOptions=array(
            'src'=>$url
        );
        $htmlOptions=array_merge($defaultHtmlOptions,$htmlOptions);
        return self::tag('script',$htmlOptions,'');
    }
}

And drop it into the components folder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question