2012-04-24 2 views
1

는 하나 개의보기에다중 headScript()을 출력

<?php echo $this->headScript(); ?> 

복수 갖는 것이 가능하다?

<?php $this->headScript()->appendFile('foo.js'); ?> 
<?php echo $this->headScript(); ?> 

some other html here 

<?php $this->headScript()->appendFile('bar.js'); ?> 
<?php echo $this->headScript(); ?> 

현재는 foo.js 중복

처럼

, 그래서 headScript 컨테이너를 청소하는 방법은 무엇입니까?

UPD는 :

정확한 문제는 내가 어떻게 <?php $this->headScript()->captureStart(); ?> 작품에 만족 아니에요 것입니다. 왜냐하면 나는 <script type="...">을 지정할 수 없기 때문에 내 IDE는 captureStartcaptureEnd 사이의 코드를 자바 스크립트로 취급하지 않습니다. 나는 별도의 파일로 JS를 이동하는 것이 낫다는 것을 알고 있지만이 특정 장소에 나는 그것이 지정 인라인이 필요합니다

그래서 나는 그들 사이 <script type="text/javascript">으로, 2 개 부분으로

PS를 출력을 분할 할

+0

가장 좋은 추측 ... 자바 스크립트를 입력 한 다음 PHP를 추가하십시오. 이 headScript 및 인라인 스크립트 도우미를 사용하는 자바 스크립트는 결국 PHP 문자열이되는 경향이 있습니다. 다른 IDE를 사용합니까 ??? 나는 그 밖의 아무것도 가지지 않고있다. – RockyFord

+0

@RockyFord : IDE가 그런 것들을 결정할 수 있을지 궁금해합니다. – zerkms

+0

이 작업을 수행 할 수 있습니다. 자세한 내용은 아래를 참조하십시오. –

답변

2

이유가 없기 때문에 setFile 대신 appendFile을 사용할 수 있습니까?

+0

무엇이 변경됩니까? – zerkms

+0

setFile은 기존 스크립트를 주어진 스크립트로 바꿉니다. –

+0

아, 그래. 고마워, 내일, 휴가 후 확인합니다. 올바른 소식에 따르면 – zerkms

0

일반적으로 작동하는 방식은 <?php echo $this->headScript(); ?>입니다. headScript()를 한 번 호출하여 할당 한 모든 스크립트를 에코합니다. 보통 Boostrap에는 jquery 또는 modernizer와 같은 스크립트가 있습니다.

//BootStrap.php 
protected function _initView() { 
     //Initialize view 
     $view = new Zend_View(); 

     $view->doctype(Zend_Registry::get('config')->resources->view->doctype); 

     $view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
         'config')->resources->view->contentType); 
     $view->headLink()->setStylesheet('/css/normalize.css'); 
     $view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css'); 
     $view->headLink()->appendStylesheet('/css/blueprint/src/typography.css'); 
     $view->headLink()->appendStylesheet(
       '/javascript/mediaelement/build/mediaelementplayer.css'); 
     $view->headLink()->appendStylesheet('/css/main.css'); 
     $view->headLink()->appendStylesheet('/css/nav.css'); 
     $view->headLink()->appendStylesheet('/css/table.css'); 

     //add javascript files 
     $view->headScript()->setFile('/javascript/mediaelement/build/jquery.js'); 
     $view->headScript()->appendFile('/javascript/modernizr.js'); 

     //add it to the view renderer 
     $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
         'ViewRenderer'); 
     $viewRenderer->setView($view); 
     //Return it, so that it can be stored by the bootstrap 
     return $view; 
    } 

나는 preDispatch()에서 일반적으로 컨트롤러를 전달 단지 문제 나중에 스크립트를 추가해야하는 경우 :

public function preDispatch() { 

     if ($this->getRequest()->getActionName() == 'play') { 
      $this->_helper->layout->setLayout('play'); 
      $this->view->headScript()->appendFile(
        'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js' 
        ); 
      $this->view->headScript()->appendFile(
        '/javascript/mediaplayer/jwplayer.js' 
        ); 
     } 
    } 

한 전화 <?php echo $this->headScript(); ?> 이러한 스크립트 파일의 4를 메아리 .
인라인 스크립트를 사용하면 inlineScript() 도우미를 사용하여 같은 종류의 작업을 수행 할 수 있습니다. inlineScript() 도우미는 javascript가 아닌 다른 어딘가에 파일이 필요한 경우 사용하는 도우미입니다.

+0

그래,하지만 내가 필요한 것은 출력을 2 조각으로 나누는 것이다 : – zerkms

+0

인라인 스크립트를 사용하거나 헤드 스크립트가 포함 된 순서대로 게시 할 것이다. – RockyFord

+0

아마도 정확한 문제를 제시하면 더 많은 도움이 될 수 있습니다. – RockyFord

1

문제는 여러 .js 섹션의 분리입니다. headlink, headscript 등을위한 viewhelpers가 ArrayAccess 인터페이스를 구현하기 때문에 이것은 완전히 가능합니다.

이 내가 그것을 할 방법입니다 - (일관성이 해골에서)를 ZF2 부트 스트랩을 사용하여 :

<!-- allows for comments as well, within diff. .js script tag outputs --> 
<?php 
$this->headScript() 
    ->prependFile($this->basePath() . '/js/bootstrap.min.js') 
    ->prependFile($this->basePath() . '/js/jquery.min.js') 
    ->prependFile($this->basePath() . '/js/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 9',)) 
    ->prependFile($this->basePath() . '/js/html5shiv.js', 'text/javascript', array('conditional' => 'lt IE 9',)); 

// Notice! below we'll echo out what we have in the headScript placeholder object 
echo $this->headScript(); 

// Now, since it implements ArrayAccess interface, we can use exchangeArray() method 
// to clear out (if you will) the stored array of .js files we've previously assigned 
$this->headScript()->exchangeArray(array()); 
?> 

<!-- Some other js file(s) I have to include --> 
<?php 
$this->headScript() 
    ->appendFile($this->basePath() . '/js/scripts.js', 'text/javascript'); 

// same as above for consistency 
echo $this->headScript(); 
$this->headScript()->exchangeArray(array()); 
?> 

이 엄청난 도움이 될 것입니다.

관련 문제