Changes for page Development-Custom Templates
Last modified by Pascal Robert on 2010/09/13 00:27
From version 3.1
edited by Quinton Dolan
on 2007/07/12 21:02
on 2007/07/12 21:02
Change comment:
There is no comment for this version
To version 2.1
edited by smmccraw
on 2007/07/08 09:45
on 2007/07/08 09:45
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. qdolan1 +XWiki.smmccraw - Content
-
... ... @@ -1,20 +1,21 @@ 1 1 == Petite Abeille == 2 2 3 -//The following solution is more involved than what is strictly needed. It may avoid the debug message, he is using some quasi-private stuff.// 3 +[The following solution is more involved than what is strictly needed. It may avoid 4 +the debug message, he is using some quasi-private stuff. 4 4 5 5 === It can be quite easy === 6 6 7 -{{ code}}8 +{{panel}} 8 8 9 -protected WOElement myTemplate = null; 10 -public WOElement template() { 11 - if ( myTemplate == null ) { 12 - myTemplate = WOComponent.templateWithHTMLString( html, wod, null ); 10 + protected WOElement myTemplate = null; 11 + public WOElement template() { 12 + if ( myTemplate == null ) { 13 + myTemplate = WOComponent.templateWithHTMLString( html, wod, null ); 14 + } 15 + return myTemplate; 13 13 } 14 - return myTemplate; 15 -} 16 16 17 -{{/ code}}18 +{{/panel}} 18 18 19 19 A More Involved Solution: 20 20 ... ... @@ -24,88 +24,90 @@ 24 24 25 25 * First you will need to overwrite WOComponent.template() 26 26 27 -{{ code}}28 +{{panel}} 28 28 29 -public WOElement template() { 30 - return Component.templateForComponent( this ); 31 -} 30 + public WOElement template() { 31 + return Component.templateForComponent( this ); 32 + } 32 32 33 -{{/ code}}34 +{{/panel}} 34 34 35 35 * Now, templateForComponent could look like this: 36 36 37 -{{ code}}38 +{{panel}} 38 38 39 -public static WOElement templateForComponent(WOComponent aComponent) { 40 -if ( aComponent != null ){ 41 - String aName = aComponent.name(); 42 - WOElement anElement = (WOElement) _elementCache.get( aName ); 43 - if ( anElement == null ) { 44 - String anHTMLString = Component.componentTemplateWithExtension( aComponent, ".html" ); 45 - String aDescriptionString = Component.componentTemplateWithExtension( aComponent, ".wod" ); 46 - anElement = WOComponent.templateWithHTMLString ( anHTMLString, aDescriptionString, null ); 47 - if ( aComponent.isCachingEnabled() == true ) { 48 - _elementCache.put( aName, anElement ); 40 + public static WOElement templateForComponent(WOComponent aComponent) { 41 + { 42 + String aName = aComponent.name(); 43 + WOElement anElement = (WOElement) _elementCache.get( aName ); 44 + if ( anElement == null ) { 45 + String anHTMLString = Component.componentTemplateWithExtension( aComponent, ".html" ); 46 + String aDescriptionString = Component.componentTemplateWithExtension( aComponent, ".wod" ); 47 + anElement = WOComponent.templateWithHTMLString ( anHTMLString, aDescriptionString, null ); 48 + if ( aComponent.isCachingEnabled() == true ) { 49 + _elementCache.put( aName, anElement ); 50 + } 49 49 } 52 + return anElement; 50 50 } 51 - return anElement; 52 -} 53 -throw new IllegalArgumentException ( "Component.templateForComponent: null component." ); 54 -} 54 + throw new IllegalArgumentException ( "Component.templateForComponent: null component." ); 55 55 56 -{{/ code}}56 +{{/panel}} 57 57 58 +} 59 + 58 58 * The componentTemplateWithExtension method could look like the following: 59 59 60 -{{ code}}62 +{{panel}} 61 61 62 -private static String componentTemplateWithExtension(WOComponent aComponent, String anExtension) { 63 -if ( anExtension != 64 - 65 - 66 - 67 - StringBuffer aBuffer = new StringBuffer(); 68 - try { 69 - BufferedInputStream aBufferedStream = new BufferedInputStream( anInputStream ); 70 - InputStreamReader aStreamReader = new InputStreamReader( aBufferedStream ); 71 - int aChar = -1; 72 - while ( ( aChar = aStreamReader.read() ) != 73 - 74 - 75 - 76 - 77 - 78 - 79 - 80 - 81 - 82 - 83 - }84 - throw new IllegalArgumentException ( "Component.componentTemplateWithExtension: null extension." );85 -} 64 + private static String componentTemplateWithExtension(WOComponent aComponent, String anExtension) { 65 + if ( anExtension !=_null_)_{ 66 +____String_aResource_=_aComponent.name()_+_anExtension; 67 +____InputStream_anInputStream_=_Component.componentStreamForResource(_aComponent,_aResource_); 68 +____if_(_anInputStream_!= null ) { 69 + StringBuffer aBuffer = new StringBuffer(); 70 + try { 71 + BufferedInputStream aBufferedStream = new BufferedInputStream( anInputStream ); 72 + InputStreamReader aStreamReader = new InputStreamReader( aBufferedStream ); 73 + int aChar = -1; 74 + while ( ( aChar = aStreamReader.read() ) !=_-1_)_{ 75 +__________aBuffer.append(_(char)_aChar_); 76 +________} 77 +__________anInputStream.close(); 78 +________}_catch(Exception_anException)_{ 79 +__________Log.warning(_anException_); 80 +________} 81 +________return_aBuffer.toString(); 82 +______} 83 +______throw_new_RuntimeException_(_"Component.componentTemplateWithExtension:_resource_not_found:_'"_+__aResource_+_"'."_); 84 +____} 85 +____throw_new_IllegalArgumentException_(_"Component.componentTemplateWithExtension:_null_extension."_); 86 +__} 87 +__} 86 86 87 -{{/ code}}89 +{{/panel}} 88 88 89 -* And finally componentStreamForResource could be something along these: 91 +* //And//finally//componentStreamForResource//could//be//something//along//these// 92 +lines~:// 90 90 91 -{{ code}}94 +{{panel}} 92 92 93 -private static InputStream componentStreamForResource(WOComponent aComponent, String aResource) { 94 -if ( aResource != null ) { 95 - File aDirectory = new File( System.getProperty( "user.dir" ) ); 96 - File aFile = new File( aDirectory, aResource ); 97 - if ( aFile.exists() == true ) { 98 - try { 99 - return new FileInputStream( aFile ); 100 - } catch(Exception anException) { 101 - Log.warning( anException); 96 +__private_static_InputStream_componentStreamForResource(WOComponent_aComponent,_String_aResource)_{ 97 +__if_(_aResource_!= null ) { 98 + File aDirectory = new File( System.getProperty( "user.dir" ) ); 99 + File aFile = new File( aDirectory, aResource ); 100 + if ( aFile.exists() == true ) { 101 + try { 102 + return new FileInputStream( aFile ); 103 + } catch(Exception anException) { 104 + Log.warning( anException); 105 + } 102 102 } 107 + return aComponent.getClass().getResourceAsStream( aResource ); 103 103 } 104 - return aComponent.getClass().getResourceAsStream( aResource ); 105 -} 106 -throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." ); 107 -} 109 + throw new IllegalArgumentException( "Component.componentStreamForResource: null resource." ); 110 + } 108 108 109 -{{/ code}}112 +{{/panel}} 110 110 111 111 Category:WebObjects