Deprecated: Creation of dynamic property CF\WordPress\DataStore::$logger is deprecated in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php on line 23

Deprecated: Creation of dynamic property CF\WordPress\Proxy::$pluginAPI is deprecated in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/Proxy.php on line 31

Deprecated: Creation of dynamic property SyntaxHighlighter::$brush_names is deprecated in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/syntaxhighlighter/syntaxhighlighter.php on line 248

Deprecated: Creation of dynamic property SyntaxHighlighter::$specialchars is deprecated in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/syntaxhighlighter/syntaxhighlighter.php on line 326
המכללה | טמפלטים – קבצי INCLUDE
Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

טמפלטים – קבצי INCLUDE

כפי שראינו בשלב הקודם, ניתן לייצר טמפלטים לדפים הראשיים יחסית בקלות. כל שיש לעשות זה לערוך את קובץ ה css ולהכניס את התגיות המתאימות בתוך ה loop. היתרון בדרך הזו היא בפשטות ההטמעה אם כי יש בה גם חסרונות. כך למשל, היא מגבילה את המשתמש לטמפלט אחד בלבד שיופעל על כל הפוסטים בעמוד. בנוסף, היא מצריכה כתיבה מחודשת של הפונקציה בכל אחד מהעמודים ושינוי של הטמפלטים יצריך מעבר מקיף על כל הקבצים ותיקון של כל הפונקציות הרלוונטיות.

על מנת לעבוד נכון ולהקל על שינויים בעתיד נייצר קובץ ייעודי שיכיל את כל הטמפלטים שלנו כפונקציות, נטמיע אותו בתוך הדפים שלנו ונקרא לטמפלטים כפונקציות. היתרון כאמור – הוא ביכולת שלנו לשנות בקלות ובאופן גורף את כל הטמפלטים שבאתר.

כיצד
באחד מהפוסטים הקודמים עברנו על התנהגות ה include – פונקציה המאפשרת לנו לקרוא לדף php מתוך הדף הקיים ולגשת אל כל התוכן שלו.

לצורך כך, ניצור קובץ בשם templates.php וניצור בתוכו פונקציה היוצרת טריפל.

function makeTripel() 
{ 
	echo '
'; echo '
'; echo '

', the_title(), '

'; echo '', the_time('d בF Y'), ' מאת ', the_author(), ''; echo '
'; echo the_excerpt(); echo '
'; echo '
'; }

הפונקציה הזו איננה מחזירה ערך (למרות שניתן היה בפועל לבנות משתנה מחרוזת שיקבל לתוכו את כל התוכן הרלוונטי ואז יוחזר לפונקציה הקוראת (שתציג אותו שם). לצורך ההדגמה זה לא משנה ורמת היעילות לא ממש נפגעת כאן – לכן בחרתי בדרך הזו.

בעיקרון, היות ומדובר בפונקצית php כל הטקסטים צריכים להיקרא בעזרת echo על מנת להיות מוצגים. אם מדובר בטקסט בלבד (כמו למשל שורה מספר 3) זה יחסית פשוט, אבל כאשר אנחנו רוצים להציג טקסט קבוע ביחד עם טקסט המגיע "מבחוץ" צריך לשרשר את המחרוזות. שירשור ב php נעשה באמצעות אופרטור הפסיק וחשוב לשים לב שאין מחרוזת לא סגורה לפניו. כל מקבץ של מחרוזות נסגור באמצעות גרש בודד (') ויש להבדיל בין הגרש הבודד של המחרוזת לבין גרשיים (“) במקרים של קריאה לקישור או תמונה למשל.

טעויות בשרשור יניבו שלל הודעות שגיאה המכריזות על פסיק חסר או מיותר ולעיתים מציאה של התקלה עלולה להיות מפרכת ומעצבנת.

מעכשיו, על מנת ליצור עמוד של טריפלים נצטרך לקרוא לו כך:

< ?php
	include 'templates.php';
	 if (have_posts()) : 
		 while (have_posts()) : the_post(); 
			 makeTripel();
		endwhile; 
?>

הרבה יותר פשוט וברור מאשר הפונקציה המקורית שהייתה עמוסה במידע, והתוצאה הסופית נותרת זהה. ומי שרוצה לראות בפועל, יכול למצוא הדגמה לכך כאן.

לדרג את הפוסט
0

Posted

in

by

Tags:

Comments

תגובה אחת על “טמפלטים – קבצי INCLUDE”

  1. תמונת פרופיל של יחיאל

    ניצן ערב טוב לך !
    זה הנודניק שבעבר הציק לך בקשר ליצירת הטריפל …
    עשיתי כל מה שכתבת כאן … יצרתי קובץ טמפלט …והכנסתי את קובץ הקריאה לתוך קובץ האינדקס (במקום הנוכחי) ומשום מה שום דבר לא זז …
    האם תוכל להסביר בצורה יותר מדוייקת איפה לכתוב את הקודים שציינת לעיל ומה לכתוב בדיוק ???
    האם יש גם אפשרות לשים את הפוסטים (במקום בצורה של הטריפל – 3 בעמודה אחת) בצורה מאוזנת, אחד מתחת לשני, כך שתציג רק את הסרטון בקטן ואת התוכן בקצרה ??

    תודה מראש

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *


Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/DataStore.php:23) in /home/nitzan_n2b/n2b.org/wordpress/wp-content/plugins/cloudflare/src/WordPress/HTTP2ServerPush.php on line 79