異世界


2014年8月17日 星期日

Smarty 入門–php

 

 

資料參考來源: http://blog.xuite.net/ghostjackyo/WorkTest/37454693-Smarty+%E5%85%A5%E9%96%80

序言

剛開始接觸樣版引擎的 PHP 設計師,聽到 Smarty 時,都會覺得很難。其實筆者也不例外,碰都不敢碰一下。但是後來在剖析 XOOPS 的程式架構時,開始發現 Smarty 其實並不難。只要將 Smarty 基礎功練好,在一般應用上就已經相當足夠了。當然基礎能打好,後面的進階應用也就不用怕了。

這次的更新,主要加上了一些概念性的東西,當然也有一些進階的技巧。不過這些也許早已深入大家的程式之中,如果有更好的觀點,也歡迎大家能夠回饋。文章中的程式架構僅供參考,並不是最好的作法。不過如果剛學習Smarty,倒是可以先以這樣的寫法為基礎。

jaceju@gmail 2005/03/12

Smarty介紹

什麼是樣版引擎

不知道從什麼時候開始,有人開始對 HTML 內嵌入 Server Script 覺得不太滿意。然而不論是微軟的 ASP 或是開放源碼的 PHP,都是屬於內嵌 Server Script 的網頁伺服端語言。因此也就有人想到,如果能把程式應用邏輯 (或稱商業應用邏輯) 與網頁呈現 (Layout) 邏輯分離的話,是不是會比較好呢?

其實這個問題早就存在已久,從互動式網頁開始風行時,不論是 ASP 或是 PHP 的使用者都是身兼程式開發者與視覺設計師兩種身份。可是通常這些使用者不是程式強就是美工強,如果要兩者同時兼顧,那可得死掉不少腦細胞…

所以樣版引擎就應運而生啦!樣版引擎的目的,就是要達到上述提到的邏輯分離的功能。它能讓程式開發者專注於資料的控制或是功能的達成;而視覺設計師則可專注於網頁排版,讓網頁看起來更具有專業感!因此樣版引擎很適合公司的網站開發團隊使用,使每個人都能發揮其專長!

就筆者接觸過的樣版引擎來說,依資料呈現方式大概分成:需搭配程式處理的樣版引擎和完全由樣版本身自行決定的樣版引擎兩種形式。

在需搭配程式處理的樣版引擎中,程式開發者必須要負責變數的呈現邏輯,也就是說他必須把變數的內容在輸出到樣版前先處理好,才能做 assign 的工作。換句話說,程式開發者還是得多寫一些程式來決定變數呈現的風貌。而完全由樣版本身自行決定的樣版引擎,它允許變數直接 assign 到樣版中,讓視覺設計師在設計樣版時再決定變數要如何呈現。因此它就可能會有另一套屬於自己的樣版程式語法 (如 Smarty) ,以方便控制變數的呈現。但這樣一來,視覺設計師也得學習如何使用樣版語言。

 

More……

2014年8月9日 星期六

C# 嵌入檔案包在EXE裡

合併 .net 組件 / 將 .dll 嵌入 .exe

C# 嵌入檔案,包在EXE裡

程式的追蹤記錄工具 : NLog - Advanced .NET Logging

參考資料來源 :  http://blog.miniasp.com/post/2010/07/18/Useful-Library-NLog-Advanced-NET-Logging.aspx

最近這陣子已經都改用 NLog 當作 .NET 應用程式的追蹤記錄工具 ,使用方式log4net 非常像,不過設定檔簡單多了,非常容易上手,而且 NLog 支援非常多種儲存記錄的形式(Target),例如儲存至檔案資料庫ConsoleEvent Log 或透過 EmailTCP 或 UDPSOAP (Web Service)、MSMQ 傳送出去等等非常多種形式,除此之外,在 Visual Studio 中還支援設定檔的 Intellisense 支援。

※ 下載 NLog

下載網址:http://nlog.codeplex.com/Release/ProjectReleases.aspx

建議可下載含有自動安裝的封裝檔 NLog-1.0-Refresh-setup.exe

More…

2014年8月7日 星期四

SNMP-Walk應用_多工簡單化 : async & await

參考資料 : http://www.codeproject.com/Articles/660482/Insides-Of-Async-Await 

在一個 async Methard ( )  中,程式會在 await 中等待新的線程完成,但主線程不阻塞。

該 async Methard ( ) 可多次進入,產生多個線程同時執行。

/* 主線程 */
private async void ThreadMathed()
        {
            txtAnalysisResult.Text = "Please wait while analysing the population.";
 
            try
            {
                int result = await new AnalysisEngine().AnalyzePopulationAsync();
                txtAnalysisResult.Text = result.ToString();
            }
            catch (System.Exception exp)
            {
                txtAnalysisResult.Text = exp.Message + Thread.CurrentThread.Name;
            }
        }
 
/* 子線程 Class */
class AnalysisEngine
    {
        public Task<int> AnalyzePopulationAsync()
        {
            Task<int> task = new Task<int>(AnalyzePopulation);
            task.Start();
 
            return task;
        }
 
        public int AnalyzePopulation()
        {
            //Sleep is used to simulate the time consuming activity.
            Thread.Sleep(10000);
            return new Random().Next(1, 5000);
        }
    }

 


運用到多線程 SNMP Walk : (該程式需使用 SnmpSharpNet.dll )



/* 主線程 */
        private async void button2_Click(object sender, EventArgs e)
        {
            Text = cnt++.ToString();
 
            NmsDevice dev = new NmsDevice();
            dev.IP = txtIP.Text;
            dev = await Pulling(dev, textBox1.Text);
 
            txtAnalysisResult.Text += dev.PullingOidResult + Environment.NewLine;
        }
        private async Task<NmsDevice> Pulling(NmsDevice dev, string oid)
        {
            NmsDevice Dev = new NmsDevice();
            try
            {
                Dev = await new SNMP_Walk(dev, oid).OidPullingAsync();
            }
            catch (System.Exception exp)
            {
                //txtAnalysisResult.Text = exp.Message + Thread.CurrentThread.Name;
                Dev.PullingOidResult = string.Format("No response received from SNMP agent.{0}", Environment.NewLine);
            }
            return Dev;
        }

子線程物件:



public class SNMP_Walk
 {
     /* 建構式 */
     public SNMP_Walk(NmsDevice dev, string oid)
     {
         mDevice = dev;
         OID = oid;
     }
 
     /* 屬性 */
     private NmsDevice mDevice = new NmsDevice();
     private string OID { get; set; }
 
     /* 方法 */
     public Task<NmsDevice> OidPullingAsync()
     {
         /* 一般程式寫法 */
         //Task<NmsDevice> task = new Task<NmsDevice>(SnmpWalk);
         //task.Start();
 
         /* 黏巴達 語法 */
         //Task<NmsDevice> task = Task<NmsDevice>.Factory.StartNew(() => { return SnmpWalk(); });
         //return task;
 
         /* 匿名方式 黏巴達 語法*/
         return Task<NmsDevice>.Factory.StartNew(() => { return SnmpWalk(); });
     }
     private NmsDevice SnmpWalk()
     {
         string IP = mDevice.IP;
 
         List<string> list = new List<string>();
         list = SnmpWalkList(IP, OID);
 
         /* 串接回傳值 */
         mDevice.PullingOidResult = "";
         foreach (string ss in list)
         {
             mDevice.PullingOidResult += string.Format("{0}{1}", ss, Environment.NewLine);
         }
         return mDevice;
     }
     private List<string> SnmpWalkList(string IP, string oid)
     {
         List<string> list = new List<string>();
 
         try
         {
             // SNMP community name
             OctetString community = new OctetString("public");
 
             // Define agent parameters class
             AgentParameters param = new AgentParameters(community);
             // Set SNMP version to 1
             param.Version = SnmpVersion.Ver1;
             // Construct the agent address object
             // IpAddress class is easy to use here because
             //  it will try to resolve constructor parameter if it doesn't
             //  parse to an IP address
             IpAddress agent = new IpAddress(IP);
 
             // Construct target
             UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
 
             // Define Oid that is the root of the MIB
             //  tree you wish to retrieve
             Oid rootOid = new Oid(oid.Replace("\"", "")); // ifDescr
 
             // This Oid represents last Oid returned by
             //  the SNMP agent
             Oid lastOid = (Oid)rootOid.Clone();
 
             // Pdu class used for all requests
             Pdu pdu = new Pdu(PduType.GetNext);
 
             #region Loop through results
             while (lastOid != null)
             {
                 // When Pdu class is first constructed, RequestId is set to a random value
                 // that needs to be incremented on subsequent requests made using the
                 // same instance of the Pdu class.
                 if (pdu.RequestId != 0)
                 {
                     pdu.RequestId += 1;
                 }
                 // Clear Oids from the Pdu class.
                 pdu.VbList.Clear();
                 // Initialize request PDU with the last retrieved Oid
                 pdu.VbList.Add(lastOid);
                 // Make SNMP request
                 SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
                 // You should catch exceptions in the Request if using in real application.
 
                 // If result is null then agent didn't reply or we couldn't parse the reply.
                 if (result != null)
                 {
                     // ErrorStatus other then 0 is an error returned by 
                     // the Agent - see SnmpConstants for error definitions
                     if (result.Pdu.ErrorStatus != 0)
                     {
                         // agent reported an error with the request
                         Console.WriteLine(string.Format("Error in SNMP reply. Error {0} index {1}",
                             result.Pdu.ErrorStatus,
                             result.Pdu.ErrorIndex));
                         lastOid = null;
                         break;
                     }
                     else
                     {
                         // Walk through returned variable bindings
                         foreach (Vb v in result.Pdu.VbList)
                         {
                             // Check that retrieved Oid is "child" of the root OID
                             if (rootOid.IsRootOf(v.Oid))
                             {
                                 //listBox1.Items.Add(string.Format("{0} ({1}): {2}",
                                 //    v.Oid.ToString(),
                                 //    SnmpConstants.GetTypeName(v.Value.Type),
                                 //    v.Value.ToString()) );
                                 list.Add(string.Format("{0}{1}", v.Value.ToString(), Environment.NewLine));
                                 lastOid = v.Oid;
                             }
                             else
                             {
                                 // we have reached the end of the requested
                                 // MIB tree. Set lastOid to null and exit loop
                                 lastOid = null;
                             }
                         }
                     }
                 }
                 else
                 {
                     list.Add(string.Format("No response received from SNMP agent.{0}", Environment.NewLine));
                 }
             }
             #endregion
 
             target.Close();
             return list;
         }
         catch (Exception ex)
         {
             list.Add(string.Format("No response received from SNMP agent.{0}", Environment.NewLine));
             return list;
         }
     }
 }