2011-10-05 3 views
1

변수가 grails 태그에서 구문 분석 될 때 약간의 문제가 있다고 생각합니다. 내 태그 라이브러리에서 Grails 태그가 본문에서 변수를 해석하지 않음

, 내가 _contentTag.gsp에서

def contentArea = {attrs, body ->   
    def domainObject = Class.forName("${attrs.contentType}", true, Thread.currentThread().contextClassLoader).newInstance() 
    def numberOfRows = !StringUtils.equals("${attrs.max}", "null")? new Integer("${attrs.max}") : new Integer("1"); 
    def results = domainObject.getByContentAreaKey("${attrs.contentAreaKey}", numberOfRows) 
    out << g.render(
    template: '/layouts/contentTag', 
    model: [contentAreaKey: attrs.contentAreaKey, results : results, contentNamespace: "${attrs.contentAreaKey}" + "_contentList", body:body()]) 
    out << body() 
} 

가지고, 레이아웃은 다음과 같습니다

<b>In tag layout, </b> 
<g:set var="${contentNamespace}" value="bobby"/> 
contentNamespace = ${contentNamespace}<br/><!-- prints "minicontent_contentList" --> 
minicontent_contentList = ${minicontent_contentList}<br/> <!-- prints "bobby" --> 

그리고 호출 GSP에서

, 태그가 호출됩니다

<mynamespace:contentArea var="myVar" contentAreaKey="minicontent" contentType="com.my.test.MiniContentType"> 
    <br/>Test Text<br/> 
    <b>in calling GSP,</b> 
    contentNamespace = ${contentNamespace}<br/><!-- prints nothing --> 
    minicontent_contentList = ${minicontent_contentList}<br/><!-- prints nothing -->   
</mynamespace:contentArea> 

contentNamespace 및 minicontent_contentList는 태그 본문에서 확인되지 않습니다. 변수를 해결할 수 있습니까? 그렇다면 어떻게해야합니까?

답을 얻는 데 도움이되는 경우 다른 컨트롤러를 통해 관리 할 수있는 작은 컨텐트 영역이있는 페이지가 있습니다. 콘텐츠 영역은 모두 텍스트 (링크, 그래픽 등) 뒤에 비슷한 데이터가 있지만 레이아웃은 다릅니다. 나는 페이지를 차단하기 위해 sitemesh 레이아웃을 사용했고, 호출하는 gsp는 이러한 sitemesh 컨텐츠 블록 중 하나를 나타냅니다.

나는 grails와 SO에 대한 도움을 매우 구하기 때문에 새로운 비평을 기꺼이 원하지만 친절해야한다. :)

답변

1

매개 변수로 전달 된 bodyClosure이며 메서드 및 매개 변수가 선언 된 위치 (여기서 기본 gsp가됩니다)로 해석됩니다. bodydelegate을 태그 라이브러리에 설정하고 resolveStrategyClosure.DELEGATE_FIRST으로 설정할 수 있습니다. 이렇게하면 contentNamespace을 해결할 수 있습니다.

def contentArea = {attrs, body -> 
    ... 
    def contentNamespace = "${attrs.contentAreaKey}" + "_contentList" 
    out << g.render(
    ... 
    body.delegate = this 
    body.resolveStrategy = Closure.DELEGATE_FIRST 
    out << body() 
} 

내가 대리인으로 템플릿을 지정하는 방법을 잘 모르겠어요으로 minicontent_contentlist 어렵게 될 것 해결하려면. tibrary 태그에 변수를 정의하고이를 템플릿 모델에 전달한 다음 전달 된 객체에 minicontent_contentlist 값을 할당하면 태그 라이브러리 코드의 값을 다시 업데이트하여 resolveStrategy이 작동한다고 가정 할 수 있습니다. 동일한 객체가 참조로 전달되었습니다. 당신이이 delegate 매개 변수 템플릿 객체를 할당하는 경우 확인하기 위해 템플릿 GSP 중괄호 (${}) 내부의 delegate/resolveStrategy를 할당 시도 할 수있는 마지막 옵션으로

def contentArea = {attrs, body -> 
    ... 
    def minicontent_contentList 
    out << g.render(..., model:[minicontent_contentList:minicontent_contentList]) 
    ...delegate and resolveStrategy stuff... 
} 

<b>In tag layout, </b> 
<g:set var="minicontent_contentlist" value="bobby"/> 
contentNamespace = ${contentNamespace}<br/><!-- prints "minicontent_contentList" --> 
minicontent_contentList = ${minicontent_contentList}<br/> <!-- prints "bobby" --> 

.

+0

답장을 보내 주셔서 감사합니다. 첫 번째 제안을 조금 변형 해 보았습니다.하지만 호출하는 gsp에서 contentNamespace를 가져올 수 없었습니다. 당신은 저에게 좀 더 많은 대의원을 조사하도록 지시 했으므로 조만간 돌파구를 가질 수있을 것입니다. – Jaye

관련 문제